Skip to main content

Docker GUI on Ubuntu

Docker GUI 应用,Ubuntu 上如何运行呢?

  • 操作系统: Ubuntu 18.04
  • 运行镜像: continuumio/anaconda3, based on debian

Step 1) 安装 Docker#

# update the apt package indexsudo apt-get update# install packages to allow apt to use a repository over HTTPSsudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common
# add Docker’s official GPG keycurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# set up the stable repositorysudo add-apt-repository \  "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \  $(lsb_release -cs) \  stable"
# update the apt package indexsudo apt-get update# install the latest version of Docker Engine and containerdsudo apt-get install docker-ce docker-ce-cli containerd.io

允许当前非 root 用户管理 Docker:

sudo groupadd dockersudo usermod -aG docker $USER

参考:

Step 2) 准备镜像#

拉取 OpenCV 镜像,用其显示 GUI:

docker pull joinaero/anaconda3-opencv3:1.0.0

Step 3) xhost 添加 local#

$ xhost +local:dockernon-network local connections being added to access control list

Step 4) OpenCV 预览图片#

# 运行镜像,指明 DISPLAYdocker run -it --rm \  --name myenv \  -e DISPLAY \  -e QT_X11_NO_MITSHM=1 \  -v /tmp/.X11-unix:/tmp/.X11-unix \  -v $HOME/.Xauthority:/root/.Xauthority \  joinaero/anaconda3-opencv3:1.0.0
# 激活 myenv 环境conda activate myenv
# 预览 GoCoding.pngpython - <<EOFimport cv2while True:  im = cv2.imread("/tmp/GoCoding.png")  im = cv2.resize(im, (256, 256))  cv2.imshow("GoCoding", im)  key = cv2.waitKey(10) & 0xFF  if key == 27 or key == ord('q'):    breakEOF

Step 5) OpenCV 预览相机#

# docker --device 指明 video 设备docker run -it --rm \  --name myenv \  -e DISPLAY \  -e QT_X11_NO_MITSHM=1 \  -v /tmp/.X11-unix:/tmp/.X11-unix \  -v $HOME/.Xauthority:/root/.Xauthority \  --device /dev/video0 \  --device /dev/video1 \  joinaero/anaconda3-opencv3:1.0.0
# 激活 myenv 环境conda activate myenv
# 预览相机图像python - <<EOFimport sysimport cv2cap = cv2.VideoCapture(0)while True:  ret, frame = cap.read()  if not ret:    sys.exit("Read the next frame failed")  cv2.imshow("GoCoding", frame)  key = cv2.waitKey(10) & 0xFF  if key == 27 or key == ord('q'):    breakcap.release()EOF