- 操作系统: Ubuntu 18.04
- 运行镜像: continuumio/anaconda3, based on debian
Step 1) 安装 Docker
# update the apt package index
sudo apt-get update
# install packages to allow apt to use a repository over HTTPS
sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common
# add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# set up the stable repository
sudo add-apt-repository \\
"deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \\
$(lsb_release -cs) \\
stable"
# update the apt package index
sudo apt-get update
# install the latest version of Docker Engine and containerd
sudo apt-get install docker-ce docker-ce-cli containerd.io
允许当前非 root 用户管理 Docker:
sudo groupadd docker
sudo usermod -aG docker $USER
参考:
- Install Docker Engine on Ubuntu:
https://docs.docker.com/engine/install/ubuntu/ - Docker CE 清华源:
https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/ - Post-installation steps for Linux:
https://docs.docker.com/engine/install/linux-postinstall/
Step 2) 准备镜像
拉取 OpenCV 镜像,用其显示 GUI:
docker pull joinaero/anaconda3-opencv3:1.0.0
Step 3) xhost 添加 local
$ xhost +local:docker
non-network local connections being added to access control list
Step 4) OpenCV 预览图片
# 运行镜像,指明 DISPLAY
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 \\
joinaero/anaconda3-opencv3:1.0.0
# 激活 myenv 环境
conda activate myenv
# 预览 GoCoding.png
python - <<EOF
import cv2
while 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\'):
break
EOF
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 - <<EOF
import sys
import cv2
cap = 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\'):
break
cap.release()
EOF
结语
Go coding!
分享 Coding 中实用的小技巧、小知识!欢迎关注,共同成长!