Help Center/ ModelArts/ Image Management/ Creating a Custom Image for Model Training/ Creating a Custom Training Image Using a Preset Image
Updated on 2026-07-02 GMT+08:00

Creating a Custom Training Image Using a Preset Image

Principles

If you use a preset image to create a training job and you need to modify or add some software dependencies based on the preset image, you can create a custom image. In this case, on the training job creation page, select a preset image and choose Customize from the framework version drop-down list.

The process of this method is the same as that of creating a training job based on a preset image. For example:

  • The system automatically injects environment variables, as shown below:
    • PATH=${MA_HOME}/anaconda/bin:${PATH}
    • LD_LIBRARY_PATH=${MA_HOME}/anaconda/lib:${LD_LIBRARY_PATH}
    • PYTHONPATH=${MA_JOB_DIR}:${PYTHONPATH}
  • The selected boot file will be automatically started using Python commands. Ensure that the Python environment is correct. The PATH environment variable is automatically injected. Run the following commands to check the Python version for the training job:
    • export MA_HOME=/home/ma-user; docker run --rm {image} ${MA_HOME}/anaconda/bin/python -V
    • docker run --rm {image} $(which python) -V
  • The system automatically adds hyperparameters associated with the preset image.

Creating a Training Image Using a Preset Image

ModelArts provides deep learning-powered base images such as TensorFlow, PyTorch, and MindSpore images. In these images, the software mandatory for running training jobs has been installed. If the software in the base images cannot meet your service requirements, create new images based on the base images and use the new images to create training jobs.

Perform the following operations to create an image using a training base image:

  1. Install Docker. If the docker images command is executed, Docker has been installed. In this case, skip this step.

    The following uses Linux x86_64 as an example to describe how to obtain the Docker installation package. For more details, see Docker official documents.

    curl -fsSL get.docker.com -o get-docker.sh
    sh get-docker.sh
  2. Create a folder named context.
    mkdir -p context
  3. Obtain the pip.conf file.
    [global]
    index-url = https://repo.huaweicloud.com/repository/pypi/simple
    trusted-host = repo.huaweicloud.com
    timeout = 120
  4. Create an image based on a training base image provided by ModelArts. Save the edited Dockerfile in the context folder. For details about how to obtain a training base image, see ModelArts Unified Images.
    FROM {Path to the training base image provided by ModelArts}
    
    # Configure pip.
    RUN mkdir -p /home/ma-user/.pip/
    COPY --chown=ma-user:ma-group pip.conf /home/ma-user/.pip/pip.conf
    
    # Configure the preset environment variables of the container image.
    # Add the Python interpreter path to the PATH environment variable.
    # Set PYTHONUNBUFFERED to 1 to prevent log loss.
    ENV PATH=${ANACONDA_DIR}/envs/${ENV_NAME}/bin:$PATH \
        PYTHONUNBUFFERED=1
    
    RUN /home/ma-user/anaconda/bin/pip install --no-cache-dir numpy
  5. Create an image. Run the following command in the directory where the Dockerfile is stored to build the container image training:v1:
    docker build . -t training:v1
  6. Upload the new 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 environment as the root user and enter the login command. For details, see Creating a Custom Image on ECS and Using It.
      Figure 1 Login command executed on the ECS
    4. Log in to SWR and run the docker tag command to add tags to the image to be uploaded. Replace the organization name deep-learning in the following command with the actual organization name obtained inprevious operations.
      sudo docker tag tf-1.13.2:latest swr.<actual-domain-name>.com/deep-learning/tf-1.13.2:latest
    5. Run the docker push command to upload the image.
      sudo docker push swr.<actual-domain-name>.com/deep-learning/tf-1.13.2:latest
    6. After the image is uploaded, choose My Images in navigation pane on the left of the SWR console to view the uploaded custom images.

      SWR URL of the custom image: swr.<region>.myhuaweicloud.com/deep-learning/tf-1.13.2:latest

  7. Create a training job on ModelArts.
    1. Log in to the ModelArts console. In the navigation pane on the left, perform the following operations:
      • New console: Choose Model Build > Training.
      • Old console: Choose Model Training > Training Jobs.
      • Click Create Training Job and set parameters. The configuration methods for the new and old consoles are as follows:
        • New console: For details about how to create a training job, see Table 1. For details about other parameters for creating a training job, see Creating a Training Job.
          Table 1 Training configuration

          Parameter

          Description

          Preset Template (Optional)

          Click Select Preset Template to filter templates by type (currently supporting text generation and image understanding) or brand (currently supporting Qwen).

          After you select a preset template, some templates will automatically fill in the description (optional), image, boot command code directory, local code directory, and environment variables of the current job. Refer to the actual GUI for final details.

          You can also adjust the configuration as needed.

          Select Image

          Specifies the container image used to run the training code. The following options are available:

          Preset Images: Ready-to-use images provided by ModelArts that include popular frameworks (e.g., PyTorch 1.8, TensorFlow 2.1). Ideal for most standard scenarios.

          Custom Images: Select an image that you have created and pushed to the SWR image repository or a registered image.

          • You can select an image from SWR Basic Edition or Enterprise Edition.
          • Registered Images: Select an image that you have registered on the ModelArts console.

          Custom images must be registered in ModelArts Image Management before use. This option is recommended when preset base images do not meet specific dependency requirements.

          Boot Command

          Defines the command executed upon container startup to launch your training script.
          • If the training boot script is a .py file, train.py for example, the boot command is as follows.
            python ${MA_JOB_DIR}/demo-code/train.py
          • If the training boot script is an .sh file, main.sh for example, the boot command is as follows:
            bash ${MA_JOB_DIR}/demo-code/main.sh

          The boot command supports multiple commands concatenated with ; or &&. Note that demo-code represents the leaf directory of the OBS path where your code is stored; adjust this according to your actual project structure.

          NOTE:

          To ensure data security, do not include sensitive information such as plaintext passwords.

          Code Directory (Optional)

          Specifies the OBS directory containing the training code. This parameter is required when using a preset image and optional when using a custom image. You can choose your own OBS bucket or enter a path. The path must start with obs:// and end with a slash (/), like this: obs://bucketname/path/. For shared buckets from other users, you must enter the path. In the OBS bucket, files with the .txt, .py, .sh, and .yaml extensions can be edited online, and files with the .log, .json, and .md extensions can be viewed online.

          • Upload code to the OBS bucket beforehand. The total size of files in the directory cannot exceed 5 GB, the number of files cannot exceed 1,000, and the folder depth cannot exceed 32. If there is a pre-trained model, put it in the code directory.
          • The training code file is automatically downloaded to the ${MA_JOB_DIR}/demo-code directory of the training container when the training job is started. demo-code is the last-level OBS directory for storing the code. For example, if Code Directory is set to /test/code, the training code file is downloaded to the ${MA_JOB_DIR}/code directory of the training container.

          Code Backup Directory (Optional)

          Specifies the OBS directory where you want to back up the training code file.

          • You must create the directory in advance.
          • When a training job is started, files in the code directory are automatically backed up to ${MA_RECORD_CODE_DIR_OBS}/${ma_job_name}-${MA_TASK_NAME}-${task_index}/user-job-dir/demo-code. demo-code is the last-level OBS directory for storing the code. For example, if the code backup directory is /test/record-dir and the code directory is /test/code, the training code file will be backed up to the /test/record-dir/job_name-work-0/user-job-dir/code directory.

          Local Code Directory

          Specifies the local directory within the training container where the code will be downloaded. The default path is /home/ma-user/modelarts/user-job-dir.

          Cannot be under /home/ma-user/modelarts/*, /home/ma-user/modelarts-dev/*, /home/ma-user/infer/*, or /home/ma-user.

          Click Preview Runtime Environment to view the actual working directory of the training job.

          Environment Variable

          Add environment variables based on service requirements. For details about the environment variables preset in a training container, see Managing Environment Variables of a Training Container.

          • Click Add to add environment variables. The total number of environment variables cannot exceed 100.
          • To import environment variables in batches, click Upload. You will need to fill in the environment variables based on the provided template. The total number of environment variables should not exceed 100, or the import will fail.
          NOTE:

          To ensure data security, do not include sensitive information such as plaintext passwords.

        • Old console: For details about how to create a training job, see Table 2. For details about other parameters for creating a training job, see Creating a Production Training Job.
          Table 2 Creating a training job using a custom image

          Parameter

          Description

          Algorithm Type

          Select Custom algorithm. This parameter is mandatory.

          Boot Mode

          Mandatory. Select Custom image.

          Image

          Mandatory. Click the select button on the right and select the new image from the container image repository to upload it to SWR.

          Code Directory

          Specifies the OBS directory containing the training code. Configure this parameter only if your custom image does not contain training code.

          • Upload code to the OBS bucket beforehand. The total size of files in the directory cannot exceed 5 GB, the number of files cannot exceed 1000, and the folder depth cannot exceed 32.
          • The training code file is automatically downloaded to the ${MA_JOB_DIR}/demo-code directory of the training container when the training job is started. demo-code is the last-level OBS directory for storing the code. For example, if Code Directory is set to /test/code, the training code file is downloaded to the ${MA_JOB_DIR}/code directory of the training container.

          User ID

          User ID for running the container. The default value 1000 is recommended.

          If the UID needs to be specified, its value must be within the specified range. The UID ranges of different resource pools are as follows:

          • Public resource pool: 1000 to 65535
          • Dedicated resource pool: 0 to 65535

          Boot Command

          Mandatory. Command for booting an image.

          When a training job is running, the boot command is automatically executed after the code directory is downloaded.
          • If the training boot script is a .py file, train.py for example, the boot command is as follows.
            python ${MA_JOB_DIR}/demo-code/train.py
          • If the training boot script is an .sh file, main.sh for example, the boot command is as follows:
            bash ${MA_JOB_DIR}/demo-code/main.sh

          The boot command supports multiple commands concatenated with ; or &&. Note that demo-code represents the leaf directory of the OBS path where your code is stored; adjust this according to your actual project structure.

          Local Code Directory

          Specify the local directory of a training container. When the training starts, the system automatically downloads the code directory to this directory.

          (Optional) The default local code directory is /home/ma-user/modelarts/user-job-dir.

          Work Directory

          During training, the system automatically runs the cd command to execute the boot file in this directory.