Step 2 Creating a Custom Image
This section describes how to edit a Dockerfile, use it to create an image, and use the created image to create a notebook instance. For details about how to edit a Dockerfile, see Dockerfile reference.
Prerequisites
You have prepared a Docker server by referring to Step 1 Preparing a Docker Server and Configuring an Environment.
Querying Base Images (Skip This Step for Third-Party Images)
For details about ModelArts base images, see Notebook Base Image List. Check the image URL in the corresponding section based on the engine type of the preset image.
Creating an Image
- Access SWR.
- Log in to the SWR console.
- In the navigation pane on the left, choose Dashboard, and click Generate Login Command in the upper right corner. On the displayed page, copy the login command.
Figure 1 Obtaining the login command
- The validity period of the generated login command is 24 hours. To obtain a long-term valid login command, see Obtaining a Login Command with Long-Term Validity. After you obtain a long-term valid login command, your temporary login commands will still be valid as long as they are in their validity periods.
- The domain name at the end of the login command is the image repository address. Record the address for later use.
- Run the login command on the machine where the container engine is installed.
The message "Login Succeeded" will be displayed upon a successful login.
- Pull a base image or third-party image. The following uses a third-party image as an example.
docker pull swr.ap-southeast-1.myhuaweicloud.com/notebook-xxx/ubuntu:18.04 #Your organization name and image
- Compile a Dockerfile.
Run the vim command to create a Dockerfile. If a ModelArts base image is used, see Dockerfile on a ModelArts Base Image for details about the Dockerfile.
If a third-party image is used, add user ma-user whose UID is 1000 and user group ma-group whose GID is 100. For details, see Dockerfile on a Non-ModelArts Base Image.
In this case, PyTorch 1.8, FFmpeg 3, and GCC 8 will be installed on an Ubuntu image to build an AI image.
- Build an image.
Run the docker build command to build a new image from the Dockerfile. The description of the command parameters are as follows:
- -t specifies the new image path, including region information, organization name, image name, and version. Set this parameter based on the real-life scenario. Use a complete SWR address for debugging and registration.
- -f specifies the Dockerfile name. Set this parameter based on the real-life scenario.
- . at the end specifies that the context is the current directory. Set this parameter based on the real-life scenario.
docker build -t swr.ap-southeast-1.myhuaweicloud.com/notebook-xxx/pytorch_1_8:v1 -f Dockerfile .
Figure 2 Image created
Dockerfile on a ModelArts Base Image
Run the vim command to create a Dockerfile. If the base image is provided by ModelArts, the content of the Dockerfile is as follows:
FROM swr.ap-southeast-1.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 on a Non-ModelArts Base Image
If a third-party image is used, add user ma-user whose UID is 1000 and user group ma-group whose GID is 100 to the Dockerfile. If UID 1000 or GID 100 in the base image has been used by another user or user group, delete the user or user group. The user and user group have been added to the Dockerfile in this case. You can directly use them.
Set the user ma-user whose UID is 1000 and user group ma-group whose GID is 100. Set the permission of /home/ma-user to 750.
Run the vim command to create a Dockerfile and add a third-party (non-ModelArts) image as the base image, for example, ubuntu 18.04. The content of the Dockerfile is as follows:
# 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 multiverse" > /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
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot