Help Center> CodeArts Build> Best Practices> Graphical Build> Using Dockerfile to Create a Docker Image
Updated on 2024-04-18 GMT+08:00

Using Dockerfile to Create a Docker Image

Background

CodeArts Build provides a large number of build actions and templates. If required dependency packages and tools are not included, you can customize Docker images, manually compile the Dockerfile to add dependencies and tools.

  • Dockerfile is a script consisting of instructions and arguments. It begins with a FROM instruction and is followed by various methods, instructions, and arguments. You can apply these instructions to the base image and create an image. These instructions simplify the E2E process, including deployment. For more information, visit the Docker official website.
  • CodeArts Build provides x86 and Arm base images based on CentOS 7 (containing various common tools) and Ubuntu 18 (containing various common tools). You can create a Dockerfile based on a base image.

This section uses a Maven build as an example to describe how to use Dockerfile to customize a simple container image and push it to SoftWare Repository for Container (SWR).

Preparations

  • Organization

    When creating an image and pushing it to SWR, specify the SWR organization name. Create an organization. For details about organization restrictions, see Notes and Constraints.

  • Project code

    Use the system template Java Maven Demo to create a code repository. For details, see Creating a Repository Using a Template.

  • Dockerfile

    Create a Dockerfile based on the base image. In this example, CentOS 7 is used as the base image.

    1. On the CodeArts Build homepage, click in the upper right corner and select Custom Build Environments.
    2. On the Custom Build Environments page, click CentOS 7-based x86 Base Image to obtain the Dockerfile corresponding to the base image.
    3. Obtain the build package directory.

      The Maven build package name is <artifactId>-<version>.packaging. By default, the build package is generated in the ./target directory.

      1. In the navigation pane, choose Code > Repo.
      2. Click the code repository name. The Code tab page is displayed.
      3. Check the coordinate definition in the pom.xml file of the code repository. As shown in the following figure, the final build package path is ./target/javaMavenDemo-1.0.jar.

    4. Use the build package path obtained in step 3 to compile the Dockerfile downloaded in step 2. Add the Maven build package to the base image by running the following command:
      COPY ./target/javaMavenDemo-1.0.jar /demo/app.jar

      This command is used to copy the build package to the demo directory of the image and name the build package app.jar.

    5. After completing the Dockerfile, upload the Dockerfile and other files required for image creation to the root directory of the code repository. For details, see Uploading Local Code to CodeArts Repo.

Procedure

  1. Choose CICD > Build.
  2. Click Create Task. On the displayed page, configure the build task information.

    Table 1 Information setting

    Parameter

    Description

    Task Name

    Enter the name of the task.

    Project

    Select or create a home project for the task.

    Code Source

    Repo: CodeArts Build pulls code from CodeArts Repo. Select a source code repository and branch created in Preparations.

    Description

    Describe the task.

  3. Click Next. The Build Template page is displayed.
  4. Select template Maven and click Next.
  5. Add the action Build Image and Push to SWR.

    In the Build with Maven action, retain default values of the parameters. In the Build Image and Push to SWR action, set the parameters as described in the following table.

    Parameter

    Description

    Action Name

    Name of a build action. It can be customized.

    Tool Version

    Select a tool version or use the default one.

    Image Repository

    By default, CodeArts Build provides the SWR repository address of each Huawei Cloud region. You do not need to change the address.

    NOTE:

    Images can be pushed to custom image repositories.

    Authorized User

    Current user. Ensure that you have the permissions to edit or manage all images in the organization. For details, see User Permissions.

    Organization

    Enter the SWR organization name created in Preparations.

    Image Name

    Name of the created image. It can be customized.

    Image Tag

    Specifies the image tag, which can be customized. You can use Image name:Tag to specify a unique image.

    Working Directory

    The context path parameter in the docker build command is the relative path to the root directory of the CodeArts Repo repository.

    When Docker builds an image, the docker build command packs all content under the context path and sends it to the container engine to help build the image.

    Dockerfile Path

    Path of the Dockerfile. Set this parameter to a path relative to the working directory. For example, if the working directory is a root directory and the Dockerfile is in the root directory, set this parameter to ./Dockerfile.

    Add Build Metadata to Image

    Add the build information to the image. After the image is created, run the docker inspect command to view the image metadata.

  6. After configuring the build steps, click Create to start the build task.
  7. After the command is executed successfully, go to SWR.
  8. Click My Images in the navigation pane, select the organization specified in the Build Image and Push to SWR action, and view the built and uploaded image.