Building a Training and Inference Container Image (Debian OSs)

This section uses the container image Ubuntu 20.04 as an example to describe how to create a CANN training and inference container image that contains the CANN software packages Toolkit/NNAE and Kernels.

This section describes how to customize a Dockerfile to create a CANN container image.

  1. Run the following command to create the ascend-cann folder in any directory (for example, /home):
    1
    mkdir ascend-cann
    
  2. Run the following command to go to the ascend-cann directory:
    1
    cd ascend-cann
    
    Prepare the following software packages in the current directory:
    Table 1 Required software or files

    Software or File

    Description

    How to Obtain

    Toolkit or NNAE

    Install either Toolkit or NNAE based on the service scenario.

    For details, see Software Package Preparation.

    Kernels

    Binary OPP, which saves the operator compile time and can be installed based on service scenarios.

  3. Run the vi Dockerfile command to create a Dockerfile and add the following content to the file: You can modify the fields related to the software package name in the script based on the OS type and the software package to be installed.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    ARG BASE=$BASE
    FROM $BASE
    
    WORKDIR /tmp
    COPY . ./
    
    ENV LD_LIBRARY_PATH=/usr/local/python3.7.5/lib:$LD_LIBRARY_PATH
    ENV PATH=/usr/local/python3.7.5/bin:$PATH
    
    # Set the environment variable of the driver path.
    ARG ASCEND_BASE=/usr/local/Ascend
    ENV LD_LIBRARY_PATH=\
    $ASCEND_BASE/driver/lib64:\
    $ASCEND_BASE/driver/lib64/common:\
    $ASCEND_BASE/driver/lib64/driver:\
    $ASCEND_BASE/driver/tools/hccn_tool/:/lib64:\
    $LD_LIBRARY_PATH
    
    SHELL ["/bin/bash","-c"]
    
    1. Preparing for the environment
    RUN apt update && \
        apt install -y gcc g++ make cmake libsqlite3-dev zlib1g-dev libssl-dev libffi-dev libbz2-dev liblzma-dev wget && \
        wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz --no-check-certificate && \
        tar -zxvf Python-3.7.5.tgz && \
        cd Python-3.7.5 && \
        ./configure --prefix=/usr/local/python3.7.5 --enable-loadable-sqlite-extensions --enable-shared && \
        make -j16 && make install && \
        cd /tmp && \
        mkdir ~/.pip && \
        echo -e '[global]\nindex-url = https://mirrors.huaweicloud.com/repository/pypi/simple\ntrusted-host = mirrors.huaweicloud.com' >> ~/.pip/pip.conf && \
        pip3 install pip==23.0.1
    
    # 2. Install the CANN. The Toolkit package is used as an example. Replace it with the software package to be installed.
    ARG CANN_PKG=Ascend-cann-toolkit_*.run
    ARG KERNEL_PKG=Ascend-cann-kernels-*.run
    RUN chmod +x $CANN_PKG && \
        ./$CANN_PKG --quiet --install --install-path=$ASCEND_BASE --install-for-all && \
        chmod +x $KERNEL_PKG && \
        ./$KERNEL_PKG --quiet --install --install-for-all && \
        pip3 install attrs && \
        pip3 install numpy==1.21.6 && \
        pip3 install decorator && \
        pip3 install sympy && \
        pip3 install cffi && \
        pip3 install pyyaml && \
        pip3 install pathlib2 && \
        pip3 install psutil && \
        pip3 install protobuf==3.20 && \
        pip3 install scipy && \
        pip3 install requests && \
        pip3 install absl-py
    
    # 3 Clean up the environment.
    RUN rm -rf /root/.cache/pip && \
        rm -rf ./*
    

    Run the :wq! command to save the file and exit.

    The current dockerfile scrip installs the dependencies of the latest or specified version. If an error is reported during the installation or the versions do not meet the requirements, install the dependencies by referring to Dependencies.

  4. Run the following command in the current directory to create an image:
    1
    docker build -t {image_name}:{new_tag} --build-arg BASE={image_name}:{tag} .
    

    Do not omit . at the end of the command. The following is a command example:

    1
    docker build -t ascend-cann:8.0.0-ubuntu --build-arg BASE=ubuntu:focal .
    
    Table 2 Command parameter description

    Parameter

    Description

    {image_name}:{new_tag}

    Name and tag of the image to be built. You are advised to set {image_name}:{new_tag} in the format of "software package:software package version-container OS", for example, ascend-cann:8.0.0-ubuntu.

    --build-arg

    Parameters in the Dockerfile.

    {image_name}:{tag}

    {image_name}:{tag} is the OS basic container image of Installation Preparations.