Setting up Docker¶
Docker-CE on Ubuntu can be setup using Docker’s official convenience script:
curl https://get.docker.com | sh \
&& sudo systemctl --now enable docker
Setting up NVIDIA Container Toolkit¶
Setup the package repository and the GPG key:
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
&& curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
&& curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
Install the nvidia-docker2 package (and dependencies) after updating the package listing:
sudo apt-get update
sudo apt-get install -y nvidia-docker2
Restart the Docker daemon to complete the installation after setting the default runtime:
sudo systemctl restart docker
At this point, a working setup can be tested by running a base CUDA container:
sudo docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi
This should result in a console output shown below:
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.51.06 Driver Version: 450.51.06 CUDA Version: 11.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 On | 00000000:00:1E.0 Off | 0 |
| N/A 34C P8 9W / 70W | 0MiB / 15109MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
推荐基于已有的docker镜像创建自己的docker镜像
这里不推荐从头完全创建镜像,推荐基于hub库中已有的镜像基础创建镜像。
例如,我需要以pytorch为基础的镜像环境,我就在dockerhub库中找我期望使用的pytorch库的镜像,docker的pytorch链接库

我拉取如下docker镜像
docker pull pytorch/pytorch:1.12.0-cuda11.3-cudnn8-runtime
输出
09db6f815738: Pull complete
ecd55c89ef07: Pull complete
4782128c6040: Pull complete
aa5c1852a419: Pull complete
Digest: sha256:1ef1f61b13738de8086ae7e1ce57c89f154e075dae0b165f7590b9405efeb6fe
Status: Downloaded newer image for pytorch/pytorch:1.12.0-cuda11.3-cudnn8-runtime
docker.io/pytorch/pytorch:1.12.0-cuda11.3-cudnn8-runtime
看一下我现在具有的镜像
docker image ls
输出
REPOSITORY TAG IMAGE ID CREATED SIZE
pytorch/pytorch 1.12.0-cuda11.3-cudnn8-runtime eb86f059e26c 4 weeks ago 5.92GB
ubuntu latest 27941809078c 7 weeks ago 77.8MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
以交互的方式启动docker镜像
docker run -it pytorch/pytorch /bin/bash
输出
root@4741d878063e:/workspace#
在交互式的终端中,输入 pip install opencv-python
root@56e9f6cdd9b6:/workspace# pip install opencv-python
Collecting opencv-python
Downloading opencv_python-4.6.0.66-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (60.9 MB)
|█████████████████▌ | 33.3 MB 88 kB/s eta 0:05:11
如果安装的opencv报错,输出如下
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
先卸载opencv在重新如下安装
pip uninstall opencv-python
pip install opencv-python-headless
查看当前在运行的容器,找到我们正在运行容器的ID
docker ps -a
输出
(base) test@test:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56e9f6cdd9b6 test/nnunet_image:v2 "/bin/bash" 13 minutes ago Up 13 minutes nice_dhawan
使用如下命令 docker commit ID REPOSITORY:TAG
docker commit 56e9f6cdd9b6 test/nnunet_image:0.0.3
我们查看我们生成的镜像
(base) test@test:~$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test/nnunet_image 0.0.3 37b05361b519 13 seconds ago 6.16GB
test/nnunet_image v2 eb86f059e26c 5 weeks ago 5.92GB
docker build -t [名字:Tag] .
这里的 . 不可以缺少,他代表当前目录
当前目录需要有Dockerfile文件
docker build -t test/nnunet_image:0.0.2 .
Dockerfile 文件内容如下
FROM pytorch/pytorch:1.12.0-cuda11.3-cudnn8-runtime
RUN pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple
RUN pip3 config set install.trusted-host mirrors.aliyun.com
RUN pip3 install opencv-python-headless
FROM代表使用的基础镜像
RUN 代表每次运行的命令
镜像打包命令格式为: docker save REPOSITORY:TAG |gzip > 镜像名,此处即为 docker save test/nnunet_image:0.0.3 |gzip > nnunet_image.tar.gz
产生的镜像文件,可以传输到其他服务器上用于环境配置
docker load < tar 包所在路径>
docker load nnunet_image.tar.gz
就可以将压缩包导入到docker的镜像中了