Updated on 2024-06-18 GMT+08:00

Uploading an Image

Introduction

SWR provides easy, secure, and reliable management for container images throughout their lifecycles, facilitating the deployment of containerized applications. This section uses the 2048 application as an example to show how to install the container engine, build an image, and use the container engine to upload the image to SWR.

  • There are two ways to upload an image to SWR: using a container engine client or the SWR console. This section describes how to use a container engine client to upload an image. For details about how to upload an image on the console, see Uploading an Image Through SWR Console.
  • Currently, there are no SWR APIs available for uploading images.

The following diagram shows the process of uploading an image to SWR.

Figure 1 Process

Prerequisites

You already have a Huawei Cloud account.

If you do not have a Huawei Cloud account, perform the following steps to create one:

  1. Visit Huawei Cloud and click Sign Up.
  2. On the page displayed, sign up for an account. After you have successfully signed up, the system automatically redirects you to your personal information page.

Step 1: Install the Container Engine

Prepare an ECS, on which Docker 1.11.2 or later is installed.

  1. Create a Linux ECS with an EIP. For details, see Purchasing and Logging In to a Linux ECS.

    For demonstration, you do not need to select high-specification ECS and public IP address. For example, select the ECS with 1 vCPUs | 2 GB, the public IP bandwidth of 1 Mbit/s, and the operating system with CentOS 7.5.

    • You can also install the container engine on other machines.
    • If you use a CentOS, you are advised to use CentOS 7, CentOS 7.2, CentOS 7.3, CentOS 7.4, CentOS 7.5 or CentOS 7.6 to avoid exceptions during the installation.

  2. After the ECS is created, return to the ECS list and click Remote Login to log in to the ECS as user root.
  3. Run the following commands 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

Step 2: Build an Image

  1. Run the following command on the ECS where Docker is installed to download the source code of the 2048 application:

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

    If the message "git: command not found" is displayed, the Git tool is not installed. In this case, run the yum install git command to install it first.

  2. After the download is successful, access the 2048 directory.

    cd 2048

  3. Modify the Dockerfile file.

    vim Dockerfile

    FROM nginx
    COPY . /usr/share/nginx/html
    
    EXPOSE 80
    CMD ["nginx", "-g", "daemon off;"]
    • FROM: specifies nginx as the base image.
    • COPY: copies the source code of the 2048 application to the directory /usr/share/nginx/html in the container.
    • EXPOSE: exposes port 80 of the container.
    • CMD: specifies the default command to run the container.

    Press Esc and enter :wq to save the settings and exit.

  4. Run the docker build command to build an image.

    docker build -t 2048 .

    In the preceding command:

    • -t indicates to label the image, that is, to name the image. In this example, the image name is 2048.
    • . indicates the context path. All contents in this path are packed and sent to the container engine to build an image.

  5. Run the following command to view the 2048 image that has been successfully built. The image tag is latest by default.

    docker images

    # docker images
    REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
    2048         latest    8d421c503ed0   About a minute ago   134MB
    nginx        latest    dd34e67e3371   6 days ago           133MB

    You can also see a nginx image, which is pulled from the image repository and used as the base image of the 2048 image.

  6. (Optional) Run the container image.

    After the image is successfully built, you can run the docker run command to run the container image.

    docker run -p 8080:80 2048

    The docker run command starts a container. In the preceding command, -p indicates that port 8080 of the VM is mapped to port 80 of the container. When you access https://EIP of the ECS:8080, the container is accessed. The 2048 application page is displayed.

Step 3: Create an Organization

An organization is used to isolate images and grant access permissions, namely, read, edit, and manage, to different users under an account.

  1. Log in to the SWR console.
  2. In the navigation pane on the left, choose Organization Management. On the displayed page, click Create Organization in the upper right corner.
  3. Enter the organization name and click OK.

    Figure 2 Creating an organization

Step 4: Connect to SWR

  1. Log in to the SWR console.
  2. In the navigation pane on the left, choose Dashboard and click Generate Login Command in the upper right corner. On the displayed page, click to copy the login command.

    Figure 3 Generating a login command

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

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

    The message Login Succeeded will be displayed upon a successful login.

Step 5: Upload the Image

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

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

    In the preceding command:

    • [Image name 1:tag 1]: name and tag of the image to be uploaded.
    • [Image repository address]: The domain name at the end of the login command in Step 4: Connect to SWR is the image repository address, which can be obtained on the SWR console.
    • [Organization name]: name of the organization created in Step 3: Create an Organization.
    • [Image name 2:tag 2]: desired image name and tag.

    Example:

    docker tag 2048:latest swr.ap-southeast-1.myhuaweicloud.com/cloud-develop/2048:v1

  2. Push the image to the image repository.

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

    Example:

    docker push swr.ap-southeast-1.myhuaweicloud.com/cloud-develop/2048:v1

    The following information will be returned upon a successful push:

    The push refers to repository [swr.ap-southeast-1.myhuaweicloud.com/cloud-develop/2048]
    fbce26647e70: Pushed 
    fb04ab8effa8: Pushed 
    8f736d52032f: Pushed 
    009f1d338b57: Pushed 
    678bbd796838: Pushed 
    d1279c519351: Pushed 
    f68ef921efae: Pushed 
    v1: digest: sha256:0cdfc7910db531bfa7726de4c19ec556bc9190aad9bd3de93787e8bce3385f8d size: 1780

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

  3. Use the pushed image to deploy a workload on CCE.