华为云AI开发平台ModelArtsStep2 制作自定义镜像_云淘科技

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

前提条件

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

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

首先配置鉴权信息,帐号、用户名、密码和局点。更多信息请查看配置登录信息。

ma-cli configure

执行后会提示用户输入信息,使用IAM用户登录的方式登录,请注意输入的password信息会被隐藏。

图1 配置鉴权信息

然后使用以下命令查询当前帐号可访问的所有镜像并展示镜像的详细信息。该命令的更多信息请参考查询镜像。

ma-cli image get-image --type=BUILD_IN -v

查询结果如图2所示。

图2 查询基础镜像

制作新镜像

连接容器镜像服务。

登录容器镜像服务控制台。
选择左侧导航栏的“总览”,单击页面右上角的“登录指令”,在弹出的页面中单击复制登录指令。

图3 获取登录指令

此处生成的登录指令有效期为24小时,若需要长期有效的登录指令,请参见获取长期有效登录指令。获取了长期有效的登录指令后,在有效期内的临时登录指令仍然可以使用。
登录指令末尾的域名为镜像仓库地址,请记录该地址,后面会使用到。

在安装容器引擎的机器中执行上一步复制的登录指令。

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

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

拉取步骤3 查询基础镜像中查询到的镜像或ModelArts提供的公共镜像(请参考预置镜像)。

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

也可以拉取其他的镜像,但如果在图2中,VIS字段是“PRIVATE”,则首先要执行SWR登录命令,再进行拉取。如何登录请参考登录SWR。

编写Dockerfile

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

Dockerfile的具体内容可参考Dockerfile文件(基础镜像为ModelArts提供)。

如果使用的基础镜像不是ModelArts提供的公共镜像,需要在Dockerfile文件中添加ModelArts指定的用户和用户组,具体可参考Dockerfile文件(基础镜像为非Model…。

构建镜像

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

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

docker build -t swr.cn-north-4.myhuaweicloud.com/sdk-test/pytorch_1_8:v2 -f Dockerfile .

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
deb http://repo.huaweicloud.com/ubuntu/ bionic-updates main restricted
deb http://repo.huaweicloud.com/ubuntu/ bionic universe
deb http://repo.huaweicloud.com/ubuntu/ bionic-updates universe
deb http://repo.huaweicloud.com/ubuntu/ bionic multiverse
deb http://repo.huaweicloud.com/ubuntu/ bionic-updates multiverse
deb http://repo.huaweicloud.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://repo.huaweicloud.com/ubuntu bionic-security main restricted
deb http://repo.huaweicloud.com/ubuntu bionic-security universe
deb 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:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud" > $HOME/.condarc && \
    echo -e "[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-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 10
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
deb http://repo.huaweicloud.com/ubuntu/ bionic-updates main restricted
deb http://repo.huaweicloud.com/ubuntu/ bionic universe
deb http://repo.huaweicloud.com/ubuntu/ bionic-updates universe
deb http://repo.huaweicloud.com/ubuntu/ bionic multiverse
deb http://repo.huaweicloud.com/ubuntu/ bionic-updates multiverse
deb http://repo.huaweicloud.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://repo.huaweicloud.com/ubuntu bionic-security main restricted
deb http://repo.huaweicloud.com/ubuntu bionic-security universe
deb 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:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2" > /home/ma-user/.condarc && \
    echo -e "[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-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

父主题: 在ECS上构建自定义镜像并在Notebook中使用

同意关联代理商云淘科技,购买华为云产品更优惠(QQ 78315851)

内容没看懂? 不太想学习?想快速解决? 有偿解决: 联系专家