更新时间:2024-04-30 GMT+08:00
分享

Step2 制作自定义镜像

这一节描述如何编写一个Dockerfile,并据此构建出一个新镜像在Notebook创建实例并使用。关于Dockerfile的具体编写方法,请参考官网

前提条件

已参考Step1 准备Docker机器并配置环境信息完成docker机器准备。

查询基础镜像(第三方镜像可跳过此步骤)

ModelArts提供的公共镜像,请参考Notebook基础镜像列表,根据预置镜像的引擎类型在对应的章节查看镜像URL。

制作新镜像

  1. 连接容器镜像服务。
    1. 登录容器镜像服务控制台。
    2. 选择左侧导航栏的“总览”,单击页面右上角的“登录指令”,在弹出的页面中单击复制登录指令。
      图1 获取登录指令
      • 此处生成的登录指令有效期为24小时,若需要长期有效的登录指令,请参见获取长期有效登录指令。获取了长期有效的登录指令后,在有效期内的临时登录指令仍然可以使用。
      • 登录指令末尾的域名为镜像仓库地址,请记录该地址,后面会使用到。
    3. 在安装容器引擎的机器中执行上一步复制的登录指令。

    登录成功会显示“Login Succeeded”。

  1. 拉取基础镜像或第三方镜像(此处以基础镜像举例,第三方镜像直接替换镜像地址)。

    拉取ModelArts提供的公共镜像(请参考预置镜像)。

    docker pull swr.cn-north-4.myhuaweicloud.com/atelier/notebook2.0-pytorch-1.4-kernel-cp37:3.3.3-release-v1-20220114
  1. 编写Dockerfile。

    vim一个Dockerfile,如果使用的基础镜像是ModelArts提供的公共镜像,Dockerfile的具体内容可参考Dockerfile文件(基础镜像为ModelArts提供)

    如果使用的基础镜像是第三方镜像(非ModelArts提供的公共镜像),Dockerfile文件中需要添加uid为1000的用户ma-user和gid为100的用户组ma-group,具体可参考Dockerfile文件(基础镜像为非ModelArts提供)

    本例的Dockerfile将基于PyTorch基础镜像安装pytorch 1.8, ffmpeg 3和gcc 8,构建一个面向AI任务的镜像。

  1. 构建镜像

    使用docker build命令从Dockerfile构建出一个新镜像。命令参数解释如下:

    • “-t” 指定了新的镜像地址,包括{局点信息}/{组织名称}/{镜像名称}:{版本名称},请根据实际填写。建议使用完整的swr地址,因为后续的调试和注册需要使用。
    • “-f ”指定了Dockerfile的文件名,根据实际填写。
    • 最后的“ . ”指定了构建的上下文是当前目录,根据实际填写。
    docker build -t swr.cn-north-4.myhuaweicloud.com/sdk-test/pytorch_1_8:v1 -f Dockerfile .
    图2 构建成功

Dockerfile文件(基础镜像为ModelArts提供)

vim一个Dockerfile文件。基础镜像为ModelArts提供的镜像时,Dockerfile文件的具体内容如下:

FROM swr.cn-north-4.myhuaweicloud.com/atelier/notebook2.0-pytorch-1.4-kernel-cp37:3.3.3-release-v1-20220114

USER root
# section1: config apt source
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
    echo -e "deb http://repo.huaweicloud.com/ubuntu/ bionic main restricted\ndeb http://repo.huaweicloud.com/ubuntu/ bionic-updates main restricted\ndeb http://repo.huaweicloud.com/ubuntu/ bionic universe\ndeb http://repo.huaweicloud.com/ubuntu/ bionic-updates universe\ndeb http://repo.huaweicloud.com/ubuntu/ bionic multiverse\ndeb http://repo.huaweicloud.com/ubuntu/ bionic-updates multiverse\ndeb http://repo.huaweicloud.com/ubuntu/ bionic-backports main restricted universe multiverse\ndeb http://repo.huaweicloud.com/ubuntu bionic-security main restricted\ndeb http://repo.huaweicloud.com/ubuntu bionic-security universe\ndeb http://repo.huaweicloud.com/ubuntu bionic-security multiverse" > /etc/apt/sources.list && \
    apt-get update
# section2: install ffmpeg and gcc
RUN apt-get -y install ffmpeg && \
    apt -y install gcc-8 g++-8 && \
    update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8 && \
    rm $HOME/.pip/pip.conf
USER ma-user
# section3: configure conda source and pip source
RUN echo -e "channels:\n  - defaults\nshow_channel_urls: true\ndefault_channels:\n  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main\n  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r\n  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2\ncustom_channels:\n  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud\n  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud\n  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud\n  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud\n  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud\n  pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud\n  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud" > $HOME/.condarc && \
    echo -e "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple\n[install]\ntrusted-host = https://pypi.tuna.tsinghua.edu.cn" > $HOME/.pip/pip.conf
# section4: create a conda environment(only support python=3.7) and install pytorch1.8
RUN source /home/ma-user/anaconda3/bin/activate && \
    conda create -y --name pytorch_1_8 python=3.7 && \
    conda activate pytorch_1_8 && \
    pip install torch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 && \
    conda deactivate

Dockerfile文件(基础镜像为非ModelArts提供)

如果使用的镜像是第三方镜像,Dockerfile文件中需要添加uid为1000的用户ma-user和gid为100的用户组ma-group。如果基础镜像中uid 1000或者gid 100已经被其他用户和用户组占用,需要将其对应的用户和用户组删除。如下Dockerfile文件已添加指定的用户和用户组,您直接使用即可。

用户只需要设置uid为1000的用户ma-user和gid为100的用户组ma-group,并使ma-user有对应目录的读写执行权限,其他如启动cmd不需要关心,无需设置或更改。

vim一个Dockerfile文件,添加第三方镜像(即非ModelArts提供的官方镜像)为基础镜像,如以ubuntu18.04为例。Dockerfile文件的具体内容如下:

# Replace it with the actual image version.
FROM ubuntu:18.04
# Set the user ma-user whose UID is 1000 and the user group ma-group whose GID is 100
USER root
RUN default_user=$(getent passwd 1000 | awk -F ':' '{print $1}') || echo "uid: 1000 does not exist" && \
    default_group=$(getent group 100 | awk -F ':' '{print $1}') || echo "gid: 100 does not exist" && \
    if [ ! -z ${default_user} ] && [ ${default_user} != "ma-user" ]; then \
        userdel -r ${default_user}; \
    fi && \
    if [ ! -z ${default_group} ] && [ ${default_group} != "ma-group" ]; then \
        groupdel -f ${default_group}; \
    fi && \
    groupadd -g 100 ma-group && useradd -d /home/ma-user -m -u 1000 -g 100 -s /bin/bash ma-user && \
# Grant the read, write, and execute permissions on the target directory to the user ma-user.
chmod -R 750 /home/ma-user

#Configure the APT source and install the ZIP and Wget tools (required for installing conda).
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
    echo "deb http://repo.huaweicloud.com/ubuntu/ bionic main restricted\ndeb http://repo.huaweicloud.com/ubuntu/ bionic-updates main restricted\ndeb http://repo.huaweicloud.com/ubuntu/ bionic universe\ndeb http://repo.huaweicloud.com/ubuntu/ bionic-updates universe\ndeb http://repo.huaweicloud.com/ubuntu/ bionic multiverse\ndeb http://repo.huaweicloud.com/ubuntu/ bionic-updates multiverse\ndeb http://repo.huaweicloud.com/ubuntu/ bionic-backports main restricted universe multiverse\ndeb http://repo.huaweicloud.com/ubuntu bionic-security main restricted\ndeb http://repo.huaweicloud.com/ubuntu bionic-security universe\ndeb http://repo.huaweicloud.com/ubuntu bionic-security multivers e" > /etc/apt/sources.list && \
apt-get update && \
apt-get install -y zip wget

#Modifying the system Configuration of the image (required for creating the Conda environment)
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

#Switch to user ma-user , download miniconda from the Tsinghua repository, and install miniconda in /home/ma-user.
USER ma-user
RUN cd /home/ma-user/ && \
    wget --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-4.6.14-Linux-x86_64.sh && \
    bash Miniconda3-4.6.14-Linux-x86_64.sh -b -p /home/ma-user/anaconda3 && \
    rm -rf Miniconda3-4.6.14-Linux-x86_64.sh

#Configure the conda and pip sources
RUN mkdir -p /home/ma-user/.pip && \
    echo -e "channels:\n  - defaults\nshow_channel_urls: true\ndefault_channels:\n  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main\n  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r\n  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2" > /home/ma-user/.condarc && \
    echo -e "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple\n[install]\ntrusted-host = https://pypi.tuna.tsinghua.edu.cn" > /home/ma-user/.pip/pip.conf

#Create the conda environment and install the Python third-party package. The ipykernel package is mandatory for starting a kernel.
RUN source /home/ma-user/anaconda3/bin/activate && \
    conda create -y --name pytorch_1_8 python=3.7 && \
    conda activate pytorch_1_8 && \
    pip install torch==1.8.1 torchvision==0.9.1 && \
    pip install ipykernel==6.7.0 && \
    conda init bash && \
    conda deactivate 

#Install FFmpeg and GCC
USER root
RUN apt-get -y install ffmpeg && \
    apt -y install gcc-8 g++-8
分享:

    相关文档

    相关产品