• 基于docker构建容器镜像


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


    第一步进行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
    
    • 1
    • 2

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Install the nvidia-docker2 package (and dependencies) after updating the package listing:

    sudo apt-get update
    
    • 1
    sudo apt-get install -y nvidia-docker2
    
    • 1

    Restart the Docker daemon to complete the installation after setting the default runtime:

    sudo systemctl restart docker
    
    • 1

    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
    
    • 1

    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                                                 |
    +-----------------------------------------------------------------------------+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    第二歩构建容器,创建容器镜像

    推荐基于已有的docker镜像创建自己的docker镜像
    这里不推荐从头完全创建镜像,推荐基于hub库中已有的镜像基础创建镜像。
    例如,我需要以pytorch为基础的镜像环境,我就在dockerhub库中找我期望使用的pytorch库的镜像,docker的pytorch链接库
    在这里插入图片描述
    我拉取如下docker镜像

    docker pull pytorch/pytorch:1.12.0-cuda11.3-cudnn8-runtime
    
    • 1

    输出

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    看一下我现在具有的镜像

    docker image ls
    
    • 1

    输出

    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
    
    • 1
    • 2
    • 3
    • 4

    以交互的方式启动docker镜像

    docker run -it pytorch/pytorch /bin/bash
    
    • 1

    输出

    root@4741d878063e:/workspace#
    
    • 1

    第三步在容器中安装第三方的库,例如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
    
    • 1
    • 2
    • 3
    • 4

    如果安装的opencv报错,输出如下

    ImportError: libGL.so.1: cannot open shared object file: No such file or directory
    
    • 1

    先卸载opencv在重新如下安装

    pip uninstall opencv-python
    pip install opencv-python-headless
    
    • 1
    • 2

    第四步使用commit 构建镜像

    查看当前在运行的容器,找到我们正在运行容器的ID

    docker ps -a
    
    • 1

    输出

    (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
    
    • 1
    • 2
    • 3

    使用如下命令 docker commit ID REPOSITORY:TAG

    docker commit 56e9f6cdd9b6 test/nnunet_image:0.0.3
    
    • 1

    我们查看我们生成的镜像

    (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
    
    • 1
    • 2
    • 3
    • 4

    使用Dockerfile来构建镜像

    docker build -t [名字:Tag] .
    这里的 . 不可以缺少,他代表当前目录
    当前目录需要有Dockerfile文件

    docker build -t test/nnunet_image:0.0.2 .
    
    • 1

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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
    
    • 1

    就可以将压缩包导入到docker的镜像中了

  • 相关阅读:
    C# 上传图片至共享文件夹
    shell清理日志,通过mtime筛选文件时间
    鸿鹄工程项目管理系统em Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统
    基于激励的需求响应计划下弹性微电网的短期可靠性和经济性评估(Matlab代码实现)
    CLion更改大括号位置
    开源公告|LightDiffusionFlow SD工作流保存插件
    Attention-based LSTM for Aspect-level Sentiment Classification
    JDBC-03:PreparedStatement如何实现对数据库的增删改查操作
    highcharts 堆积图
    配电网调度vr虚拟仿真应用案例汇总
  • 原文地址:https://blog.csdn.net/weixin_37707670/article/details/126103489