基于docker构建属于我们自己的容器镜像

20 篇文章 0 订阅
17 篇文章 1 订阅

基于docker-gpu构建容器,保存容器镜像


第一步进行docker的安装

  1. 不使用GPU的安装
  2. 使用GPU的安装
    使用GPU的安装需要按照英伟达开发的docker安装说明安装
    如下是ubuntu的展示示例,请在终端中依次输入以下命令

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#

第三步在容器中安装第三方的库,例如python库

在交互式的终端中,输入 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

第四步使用commit 构建镜像

查看当前在运行的容器,找到我们正在运行容器的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

使用Dockerfile来构建镜像

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的镜像中了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值