Help Center> Cloud Container Instance> Getting Started> Building an Image and Pushing It to the Image Repository
Updated on 2024-02-23 GMT+08:00

Building an Image and Pushing It to the Image Repository

To deploy an existing application on CCI, build an image for the application and push it to the image repository. Then you can pull the image when creating a workload on CCI.

Installing the Container Engine

Before pushing an image, ensure that you have installed the container engine of version 1.11.2 or later.

  1. Create a Linux ECS with a public IP address. For details, see the ECS documentation.

    For demonstration, you do not need to select high specifications for ECS and public IP address. For example, select 1 vCPUs | 2 GiB for ECS specifications, 1 Mbit/s for the IP bandwidth, and CentOS 7.6 for the operating system.

    You can also install the container engine on other machines.

  2. Go to the ECS list and click Remote Login to log in to the ECS.
  3. Run the following command to quickly install the container engine:

    curl -fsSL get.docker.com -o get-docker.sh
    sh get-docker.sh
    sudo systemctl daemon-reload
    sudo systemctl restart docker

Building an Image

The following describes how to use a Dockerfile and an Nginx image to build the 2048 image. Before building the image, you need to create a Dockerfile.

  1. Pull the Nginx image from the image repository as the base image.

    docker pull nginx

  2. Download the 2048 static web application.

    git clone https://gitee.com/jorgensen/2048.git

  3. Build a Dockerfile.

    1. Run the following command:
      vi Dockerfile
    2. Edit the Dockerfile.
      FROM nginx
      
      MAINTAINER Allen.Li@gmail.com
      COPY . /usr/share/nginx/html
      
      EXPOSE 80
      
      CMD ["nginx", "-g", "daemon off;"]
      • nginx indicates the base image. You can select the base image based on the type of the created application. For example, select a Java image as the base image to create a Java application.
      • /usr/share/nginx/html indicates the directory for the Nginx image to store the static web.
      • 80 indicates the container port.

      For details about the Dockerfile content and format, see Dockerfile reference.

  1. Build the 2048 image.

    1. Run the following command:
      docker build -t='2048' .

    The following information will be displayed upon a successful image creation:

    Figure 1 Successful image creation
    1. Query the image.
      docker images

      If the following information is displayed, the image is successfully created.

      Figure 2 Querying the image

Pushing the Image

  1. Access SWR.

    1. Log in to the management console, choose Service List > Application > SoftWare Repository for Container.
    2. In the navigation pane, choose My Images and then click Upload Through Client. On the page displayed, click Generate a temporary login command and click to copy the command.

      The validity period of the temporary login command is 24 hours. To obtain a long-term valid login command, see Obtaining a Long-Term Valid Login Command.

    3. Run the login command on the machine where the container engine is installed.

      The message login succeeded will be displayed upon a successful login.

  2. Push the image.

    1. Run the following command to label the 2048 image on the machine where the container engine is installed:

      docker tag [Image name:tag] [Image repository address]/[Organization name]/[Image name:tag]

      Example:

      docker tag 2048:latest swr.cn-north-4.myhuaweicloud.com/cloud-develop/2048:latest

      In the preceding command:

      • swr.cn-north-4.myhuaweicloud.com indicates the address of the SWR image repository.
      • cloud-develop indicates the organization name of the image.
      • 2048:latest indicates the image name and tag.
    2. Run the following command to push the image to the image repository:

      docker push [Image repository address]/[Organization name]/[Image name:tag]

      Example:

      docker push swr.cn-north-4.myhuaweicloud.com/cloud-develop/2048:latest

      The following information will be returned upon a successful push:

      6d6b9812c8ae: Pushed 
      695da0025de6: Pushed 
      fe4c16cbf7a4: Pushed 
      v1: digest: sha256:eb7e3bbd8e3040efa71d9c2cacfa12a8e39c6b2ccd15eac12bdc49e0b66cee63 size: 948

      To view the pushed image, go to the SWR console and refresh the My Images page.