Updated on 2024-10-29 GMT+08:00

Creating a Custom Image Using Dockerfile

Scenario

This example shows how to use ma-cli commands in ModelArts CLI to create and register a custom image for AI development with a preset PyTorch image. For details, see ma-cli Image Building Command.

Procedure

  1. Create a notebook instance.
  2. Create a custom image in the notebook instance.
  3. Register the image on ModelArts.
  4. Create a notebook instance and verify the new image.

Creating a Notebook Instance

  1. Log in to the ModelArts management console. In the navigation pane on the left, choose Development Workspace > Notebook. On the displayed page, click Create Notebook. Set Image to Public image and select a PyTorch image. Retain the default values for other parameters. For details, see Creating a Notebook Instance.
  2. After the notebook instance is created and in the Running state, locate it in the notebook list, and click Open in the Operation column. On the displayed JupyterLab page, click Terminal.

Creating a Custom Image in a Notebook Instance

  1. Configure authentication information, specify a profile, and enter the account information as prompted. For details, see ma-cli Authentication.

    ma-cli configure --auth PWD -P xxx

  2. Run env|grep -i CURRENT_IMAGE_NAME to query the image used by the current instance.

  1. Create an image.

    1. Obtain the SWR address of the base image.

      CURRENT_IMAGE_NAME=swr.ap-southeast-1.myhuaweicloud.com/atelier/pytorch_1_8:pytorch_1.8.0-cuda_10.2-py_3.7-ubuntu_18.04-x86_64-20220926104358-041ba2e

    1. Load an image creation template.

      Run the ma-cli image get-template command to query the image template.

      Run the ma-cli image add-template command to load the image template to the specified folder. The default path is where the current command is located. For example, load the upgrade_current_notebook_apt_packages image creation template.

      ma-cli image add-template upgrade_current_notebook_apt_packages

    2. Modify a Dockerfile.

      The Dockerfile in this example is modified based on the base PyTorch image pytorch1.8-cuda10.2-cudnn7-ubuntu18.04, the image template upgrade_current_notebook_apt_packages is loaded, and GCC and G++ are upgraded.

      After the image template is loaded, the Dockerfile will be automatically loaded in .ma/upgrade_current_notebook_apt_packages. The content is as follows and you can modify it based on your needs.

      FROM swr.ap-southeast-1.myhuaweicloud.com/atelier/pytorch_1_8:pytorch_1.8.0-cuda_10.2-py_3.7-ubuntu_18.04-x86_64-20220926104358-041ba2e
      
      # Set proxy to download internet resources
      ENV HTTP_PROXY=http://proxy.modelarts.com:80 \
          http_proxy=http://proxy.modelarts.com:80 \
          HTTPS_PROXY=http://proxy.modelarts.com:80 \
          https_proxy=http://proxy.modelarts.com:80
      
      USER root
      
      # Config apt source which can accelerate the apt package download speed. Also install ffmpeg and gcc-8 in root mode
      RUN cp -f /etc/apt/sources.list /etc/apt/sources.list.bak && \
          sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list && \
          sed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list && \
          apt update && \
          apt -y install ffmpeg && \
          apt install -y --no-install-recommends gcc-8 g++-8 && apt-get autoremove -y && \
          update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8
      
      # ModelArts requires ma-user as the default user to start image
      USER ma-user
      
    1. Build an image.

      Run the ma-cli image build command to build an image with the Dockerfile. For details about the command, see ma-cli Image Building Commands.

      ma-cli image build .ma/upgrade_current_notebook_apt_packages/Dockerfile -swr notebook-test/my_image:0.0.1 -P XXX

      The Dockerfile is stored in .ma/upgrade_current_notebook_apt_package/Dockerfile and the new image is stored in notebook-test/my_image:0.0.1 in SWR. XXX indicates the profile specified for authentication.

Registering an Image

After an image is created, register it with ModelArts image management so that the image can be used in ModelArts.

Use either of the following methods:

  • Method 1: Run the ma-cli image register command to register an image. Then, the information of the registered image is returned, including image ID and name, as shown in the following figure. For details about the command, see ma-cli Image Building Commands.
    ma-cli image register --swr-path=swr.ap-southeast-1.myhuaweicloud.com/notebook-test/my_image:0.0.1 -P XXX
    Figure 1 Registering an image

  • Method 2: Register the image on the ModelArts management console.

    Log in to the ModelArts management console. In the navigation pane on the left, select Image Management. The Image Management page is displayed.

    1. Click Register. Paste the complete SWR address or click to select a private image from SWR for registration.
    2. Set Architecture and Type based on the site requirements. The values must be those of the image source.

Creating and Using a Notebook Instance

After an image is registered, it is available for development environment creation. You can log in to the ModelArts management console, choose DevEnviron > Notebook, and select the image during creation.