Pushing 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 an application named 2048 as an example to show how to install a container engine, build an image, and use the container engine to push the image to SWR.
- There are two ways to push an image to SWR: using a container engine client or the SWR console. This section uses a container engine client as an example.
- Currently, there are no SWR APIs available for pushing images.
The following shows the process of pushing an image to SWR.
Prerequisites
You already have a Huawei Cloud account.
If you do not have a Huawei Cloud account, perform the following steps to create one:
- Visit Huawei Cloud and click Sign Up.
- Complete the registration as prompted. After you have successfully signed up, the system automatically redirects you to your personal information page.
This section uses an application named 2048 as an example to describe the process of compiling a Dockerfile based on an application, creating an image using the Dockerfile, and uploading the image to SWR. In this Dockerfile, alpine:3.7 is used as the base image to build an image for the application.
Step 1: Install a Container Engine
Prepare an ECS where Docker 1.11.2 or later will be installed.
- Create a Linux ECS with an EIP..
The ECS and EIP are for demonstration only, so they do not require high specifications. For example, select an ECS with 1 vCPU and 2-GiB memory, an EIP with 1-Mbit/s bandwidth, and an operating system of CentOS 7.5.
- You can also install a container engine on another server instead of an ECS.
- If you use CentOS, you are advised to choose CentOS 7.0, CentOS 7.2, CentOS 7.3, CentOS 7.4, CentOS 7.5, or CentOS 7.6 to avoid exceptions during the installation.
- Return to the ECS list and click Remote Login to log in to the ECS as user root.
- Run the following commands to quickly install Docker:
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
- 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. Run yum install git to install it.
- After the download is successful, access the 2048 directory.
cd 2048
- 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.
- Run docker build to build an image.
docker build -t 2048 .
In the preceding command:
- -t is used to tag the image with a name. In this example, the image name is 2048.
- . indicates the context directory. All the files in this directory will be packed and sent to the container engine to help build an image.
- Run the following command to check the 2048 image you 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 an image named nginx, which is pulled from the image repository and used as the base image of the 2048 image.
- (Optional) Run the container image.
After an image is built, you can run docker run to use it.
docker run -p 8080:80 2048
docker run starts a container. In the preceding command, -p indicates that port 8080 of the ECS is mapped to port 80 of the container. You can visit http://<EIP-of-the-ECS>:8080 to access the container. The 2048 application page will be displayed.
Step 3: Create an Organization
An organization is used to isolate images and grant access permissions (read, edit, and manage) to different IAM users under an account.
- Log in to the SWR console.
- In the navigation pane, choose Organizations. Click Create Organization in the upper right corner.
- Enter an organization name and click OK.
Parameter
Example Value
Description
Organization Name
cloud-hangzhou
Organizations are used to isolate image repositories. With each organization being limited to one company or department, you can manage images in a centralized and efficient manner.
Step 4: Connect to SWR
- Log in to the SWR console.
- In the navigation pane, choose Dashboard. Click Generate Login Command in the upper right corner. Then, click
to copy the login command.
The login command generated here is valid for 6 hours. If a long-term valid login command is required, see Obtaining a Long-Term Login or Image Push/Pull Command.
- Run the login command on the ECS where the container engine is installed.
The message Login Succeeded will be displayed upon a successful login.
Step 5: Push the Image
- On the ECS where the container engine is installed, run the following command to tag the 2048 image:
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.
- [image-repository-address]: The domain name at the end of the login command in Step 4: Connect to SWR is the image repository address.
- [organization name]: name of the organization created in Step 3: Create an Organization.
- [image-name-2:tag-2]: new image name and tag.
Example:
docker tag 2048:latest swr.eu-west-101.cloud.aspiegel.com/cloud-develop/2048:v1
- Push the image to the SWR repository.
docker push [image-repository-address]/[organization-name]/[image-name-2:tag-2]
Example:
docker push swr.eu-west-101.cloud.aspiegel.com/cloud-develop/2048:v1
The following information will be returned upon a successful push:
The push refers to repository [swr.eu-west-101.cloud.aspiegel.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.
- Use the pushed image to deploy a workload in CCE.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.