Updated on 2026-11-06 GMT+08:00

Creating a Custom Image for Training (PyTorch + Ascend)

This section describes how to create an image and use it for training on ModelArts. The AI engine used in the image is PyTorch, and the resources used for training are powered by Ascend in a dedicated resource pool.

Overview

In this example, write a Dockerfile to create a custom image on a Linux x86_64 server running Ubuntu 22.04.

Objective: Build and install container images of the following software and use the images and NPUs for training jobs on ModelArts.

  • ubuntu-22.04
  • Ascend-cann-nnae-8.5.0
  • python-3.9
  • pytorch-2.6.0
  • pytorch_npu-2.6.0.post5
  • Miniconda3-25.9.1-3

Prerequisites

A Linux virtual or physical server with Docker 26.1.3 or later versions installed is available. The server can access the Internet and function as an image creation node.

Run the docker pull, apt-get update/upgrade, and pip install commands to check whether the node can access an external open-source software repository. If so, the node can access the Internet.

  • The preceding servers must be Arm64-powered.
  • It is good practice to install Ubuntu 22.04 on the image creation node.
  • In this section, the /opt directory is used for the image creation task. Ensure that the available storage of this directory is greater than 30 GB.
  • For details about how to install Docker, see Install Docker Engine on Ubuntu. Miniconda and TFLite installation packages are provided by third parties. ModelArts is not responsible for their security issues. If you have security requirements, harden the security of these packages, release them as files with the same names, and upload them to the image creation node.

Creating a Custom Image

  1. Check the Docker engine version. Run the following command:

    docker version | grep -A 1 Engine
    The command output is as follows:
    Engine:   
     Version: 26.1.3

    Use the Docker engine of the preceding version or later to create a custom image.

  2. Create a folder named context.

    mkdir -p context

  3. Obtain the pip.conf file. In this example, the pip source provided at Huawei Mirrors is used. Its content is as follows:

    [global]
    index-url = 
    https://repo.huaweicloud.com/repository/pypi/simple
    trusted-host = repo.huaweicloud.com
    timeout = 120

  4. Download the Miniconda3 installation file. Download the Miniconda3 installation file from https://www.anaconda.com/download. Miniconda3-py39_25.9.1-3-Linux-aarch64.sh
  5. Download torch_npu-2.6.0.post5-cp39-cp39-manylinux_2_28_aarch64.whl.
  6. Store the pip source file, .list file, .run file, .whl file, and Miniconda3 installation file in the context folder, which is as follows:

    context
    ├── Miniconda3-py39_25.9.1-3-Linux-aarch64.sh
    ├── pip.conf
    ├── torch_npu-2.6.0.post5-cp39-cp39-manylinux_2_28_aarch64.whl

  7. Write the Dockerfile of the container image.

    Create an empty file named Dockerfile in the context folder and copy the following content to the file:
    # The container image build host requires public network access
    # ===== builder stage =====
    FROM ubuntu:22.04 AS builder
    ENV DEBIAN_FRONTEND=noninteractive
    # Prepare pip.conf, Miniconda installer, and torch_npu wheel package
    RUN mkdir -p /root/.pip/
    COPY pip.conf /root/.pip/pip.conf
    COPY Miniconda3-py39_25.9.1-3-Linux-aarch64.sh /tmp
    COPY torch_npu-2.6.0.post5-cp39-cp39-manylinux_2_28_aarch64.whl /tmp
    # Use the standard Jammy ARM64 sources (ports.ubuntu.com) and install the basic dependencies
    RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
        printf "deb http://ports.ubuntu.com/ubuntu-ports/ jammy main restricted universe multiverse\n\
    deb http://ports.ubuntu.com/ubuntu-ports/ jammy-updates main restricted universe multiverse\n\
    deb http://ports.ubuntu.com/ubuntu-ports/ jammy-backports main restricted universe multiverse\n\
    deb http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted universe multiverse\n" > /etc/apt/sources.list && \
        apt-get update && \
        apt-get install -y --no-install-recommends \
            zip wget ca-certificates vim curl python3 python3-pip tzdata gnupg && \
        ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
        dpkg-reconfigure --frontend noninteractive tzdata && \
        echo Y | apt-get install -y --no-install-recommends openssh-server && \
        apt-get clean && \
        rm -rf /var/lib/apt/lists/* && \
        mv /etc/apt/sources.list.bak /etc/apt/sources.list
    # Create the ma-user user (UID=1000, GID=100), handling cases where the UID/GID might already exist
    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 && \
        if ! getent group 100 > /dev/null; then \
            groupadd -g 100 ma-group; \
        fi && \
        useradd -d /home/ma-user -m -u 1000 -g 100 -s /bin/bash ma-user && \
        chmod -R 750 /home/ma-user
    # Change ownership of temporary files to ma-user (to allow subsequent rm to succeed, /tmp has sticky bit)
    RUN chown 1000:100 /tmp/Miniconda3-py39_25.9.1-3-Linux-aarch64.sh /tmp/torch_npu-2.6.0.post5-cp39-cp39-manylinux_2_28_aarch64.whl
    # Switch to ma-user to install Miniconda
    USER ma-user
    WORKDIR /home/ma-user
    # Install miniconda-py39 (default base = Python 3.9.x)
    RUN bash /tmp/Miniconda3-py39_25.9.1-3-Linux-aarch64.sh -b -p /home/ma-user/miniconda3 && \
        rm -f /tmp/Miniconda3-py39_25.9.1-3-Linux-aarch64.sh
    # Configure conda mirrors
    RUN /home/ma-user/miniconda3/bin/conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main && \
        /home/ma-user/miniconda3/bin/conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge && \
        /home/ma-user/miniconda3/bin/conda config --set show_channel_urls yes
    # Create pytorch_2_6 virtual environment (Python 3.9) and install torch/torchvision/torchaudio/torch_npu
    RUN /bin/bash -c '\
        source /home/ma-user/miniconda3/bin/activate base && \
        conda create -y --override-channels \
        -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main \
        -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge \
        -n pytorch_2_6 python=3.9 && \
        conda run -n pytorch_2_6 \
        pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn \
        torchvision==0.21.0 torchaudio==2.6.0 numpy==1.26.4 && \
        conda run -n pytorch_2_6 \
        pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn \
        torch==2.6.0 && \
        conda run -n pytorch_2_6 \
        pip install /tmp/torch_npu-2.6.0.post5-cp39-cp39-manylinux_2_28_aarch64.whl && \
         conda run -n pytorch_2_6 pip install protobuf==3.20 pyyaml importlib_metadata && \
        rm -f /tmp/torch_npu-2.6.0.post5-cp39-cp39-manylinux_2_28_aarch64.whl \
    '
    # Install CANN toolkit/ops in the pytorch_2_6 environment
    RUN /bin/bash -c '\
        source /home/ma-user/miniconda3/bin/activate base && \
        conda run -n pytorch_2_6 \
        pip install ascend-cann-toolkit==8.5.0 \
        --extra-index-url https://ascend.devcloud.huaweicloud.com/cann/pypi/simple \
        --trusted-host ascend.devcloud.huaweicloud.com && \
        conda run -n pytorch_2_6 \
        pip install ascend-cann-910b-ops==8.5.0 \
        --extra-index-url https://ascend.devcloud.huaweicloud.com/cann/pypi/simple \
        --trusted-host ascend.devcloud.huaweicloud.com \
    '
    
    RUN /bin/bash -c '\
        source /home/ma-user/miniconda3/bin/activate base && \
        conda run -n pytorch_2_6 \
        pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn \
        ipykernel==6.7.0 \
    '
    # ===== Final image =====
    FROM ubuntu:22.04
    ENV DEBIAN_FRONTEND=noninteractive
    # Install basic tools (vim/curl), using ports.ubuntu.com Jammy sources
    RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
        printf "deb http://ports.ubuntu.com/ubuntu-ports/ jammy main restricted universe multiverse\n\
    deb http://ports.ubuntu.com/ubuntu-ports/ jammy-updates main restricted universe multiverse\n\
    deb http://ports.ubuntu.com/ubuntu-ports/ jammy-backports main restricted universe multiverse\n\
    deb http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted universe multiverse\n" > /etc/apt/sources.list && \
        apt-get update && \
        apt-get install -y --no-install-recommends \
            ca-certificates vim curl && \
        apt-get clean && \
        rm -rf /var/lib/apt/lists/* && \
        mv /etc/apt/sources.list.bak /etc/apt/sources.list
    # Create the ma-user user (UID=1000, GID=100)
    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 && \
        if ! getent group 100 > /dev/null; then \
            groupadd -g 100 ma-group; \
        fi && \
        useradd -d /home/ma-user -m -u 1000 -g 100 -s /bin/bash ma-user && \
        chmod -R 750 /home/ma-user
    # Copy Miniconda from builder (including the pytorch_2_6 virtual environment)
    COPY --chown=ma-user:100 --from=builder /home/ma-user/miniconda3 /home/ma-user/miniconda3
    # CANN environment variables (CANN 8.5.0 installed under site-packages of the pytorch_2_6 environment)
    ENV CANN_HOME=/home/ma-user/miniconda3/envs/pytorch_2_6/lib/python3.9/site-packages/ascend/cann-8.5.0
    ENV PATH=/home/ma-user/miniconda3/envs/pytorch_2_6/bin:${CANN_HOME}/bin:${CANN_HOME}/compiler/ccec_compiler/bin:$PATH
    ENV LD_LIBRARY_PATH=${CANN_HOME}/aarch64-linux/lib64:${CANN_HOME}/aarch64-linux/lib64/device/lib64:${CANN_HOME}/aarch64-linux/lib64/plugin/opskernel:${CANN_HOME}/aarch64-linux/lib64/plugin/nnengine:${CANN_HOME}/opp/built-in/op_impl/ai_core/tbe/op_tiling/lib/linux/aarch64
    ENV PYTHONPATH=${CANN_HOME}/python/site-packages:${CANN_HOME}/opp/op_impl/built-in/ai_core/tbe:$PYTHONPATH
    ENV ASCEND_AICPU_PATH=${CANN_HOME}
    ENV ASCEND_OPP_PATH=${CANN_HOME}/opp
    ENV ASCEND_HOME_PATH=${CANN_HOME}
    ENV ASCEND_TOOLKIT_HOME=${CANN_HOME}
    ENV TOOLCHAIN_HOME=${CANN_HOME}
    ENV PYTHONUNBUFFERED=1
    ENV USE_TRITON=0
    # Set the default user and working directory for the container image
    USER ma-user
    WORKDIR /home/ma-user

    For details about how to write a Dockerfile, see official Docker documents.

  8. Ensure that the Dockerfile has been created. The following shows the context folder:

    context
    ├── Miniconda3-py39_25.9.1-3-Linux-aarch64.sh
    ├── pip.conf
    ├── torch_npu-2.6.0.post5-cp39-cp39-manylinux_2_28_aarch64.whl
    ├── Dockerfile
     

  9. Create the container image. Run the following command in the directory where the Dockerfile is stored to create a container image:

    docker build . -t pytorch:2.6.0-npu

    If "connection refused" or "Client.Timeout exceeded" is reported when you access https://registry-1.docker.io/v2/ during image creation, configure the Docker proxy.

    vi /etc/docker/daemon.json

    Add the following content to the file and save the file:

    {

    "registry-mirrors":[

    "https://docker.m.daocloud.io",

    "https://docker.jianmuhub.com",

    "https://huecker.io",

    "https://dockerhub.timeweb.cloud",

    "https://dockerhub1.beget.com",

    "https://noohub.ru"]

    }

    Run the systemctl daemon-reload and systemctl restart docker commands in sequence.

    Recreate the image.

    The following log shows that the image has been created.
    Successfully tagged pytorch:2.6.0-npu

Uploading an Image to SWR

  1. Log in to the SWR console and select a region.
  2. On the console homepage, click Create Organization and enter an organization name to create an organization. In this case, deep-learning is used as an example. Replace it in subsequent commands with the actual organization name.
  3. After the organization is created, click Generate Login Command on the console homepage to obtain the login command. Log in to the ECS as the root user and enter the login command. For details, see Installing Ascend-related Software (PyTorch + Ascend) on Ubuntu 20.04.
  4. Upload the image to SWR.
    1. Tag the uploaded image.
      # Replace the region, domain, as well as organization name deep-learning with the actual values.
      sudo docker tag pytorch:2.6.0-npu swr.{region-id}.{domain}/deep-learning/pytorch:2.6.0-npu
    2. Upload the image.
      # Replace the region, domain, as well as organization name deep-learning with the actual values.
      sudo docker push swr.{region-id}.{domain}/deep-learning/pytorch:2.6.0-npu
  5. After the image is uploaded, choose My Images in navigation pane on the SWR console to view the uploaded image.

Creating a Training Job on ModelArts

  1. Log in to the ModelArts console and check whether access authorization has been configured for your account. If not, configure the authorization by referring to Configuring Agency Authorization for ModelArts with One Click. For users who used AKs for authorization, you are advised to clear the existing authorization and use an agency.
  2. In the navigation pane on the left, perform the following operations:
    • New console: Choose Model Build > Training.
    • Old console: Choose Model Training > Training Jobs.
  3. On the Create Training Job page, configure parameters and click Submit.
    • Algorithm Type: Custom algorithm
    • Boot Mode: Custom image
    • Image: swr.cn-north-4.myhuaweicloud.com/deep-learning/pytorch:2.6.0-npu
    • Code Directory: directory where the boot script file is stored in OBS, for example, obs://test-modelarts/pytorch/demo-code/. The training code is automatically downloaded to the ${MA_JOB_DIR}/demo-code directory of the training container. demo-code (customizable) is the last-level directory of the OBS path.
    • Boot Command: /home/ma-user/miniconda3/bin/python ${MA_JOB_DIR}/demo-code/pytorch-verification.py. demo-code (customizable) is the last-level directory of the OBS path.
    • Resource Pool: Dedicated resource pool
    • Resource Type: Ascend with the required driver and firmware version
    • Job Log Path: OBS path to stored training logs, for example, obs://test-modelarts/pytorch/log/.
  4. Confirm the configurations of the training job and click Submit.
  5. Wait until the training job is created.

    After you submit the job creation request, the system will automatically perform operations on the backend, such as downloading the container image and code directory and running the boot command. A training job requires a certain period of time for running. The duration ranges from dozens of minutes to several hours, depending on the service logic and selected resources. You can view log information on the job details page.