☁️Step-by-Step Container Deployment in CodeArts Deploy

Ertugrul Basar
Huawei Developers
Published in
14 min readOct 13, 2023
CodeArts

🔦Introduction

Hi, everyone🙌! In this article, I will show you how to deploy a container on ECS through Huawei Cloud CodeArts Deploy. Throughout this process, we will build an image from a Dockerfile, push it into a repository, and deploy it on CodeArts Deploy. This will help us build, organize, and deploy our images through one vendor, increasing efficiency. We will use ECS (Elastic Cloud Server), SWR (SoftWare Repository for Containers), CodeArts Repo, CodeArts Artifact, and CodeArts Deploy services during this exercise. Therefore, it won’t be a demo that includes only one service. This work will not only serve as a guideline but may also help you adapt your business to the cloud. It also uses free package services, allowing you to adapt them to your daily development processes.

Before we begin the demo, I want to briefly overview ECS, SWR, and CodeArts.

1️⃣ECS: Elastic Cloud Server (ECS) is a versatile and scalable cloud computing service that empowers users to create and manage virtual servers in the cloud.

2️⃣SWR: SoftWare Repository for Container (SWR) provides easy, secure, and reliable management of container images throughout their lifecycles, featuring image push, pull, and deleting.

3️⃣CodeArts: CodeArts is a one-stop DevSecOps platform that provides out-of-the-box cloud services for requirement delivery, code commit, check, build, verification, deployment, and release throughout the entire software lifecycle. It consists of the following services: Req (Project Management), Repo (Code Hosting), Check (Code Quality), Build (Code Build), Artifact (Build Hosting), Deploy (Build Deployment), Pipeline (Continuous Delivery), TestPlan (Deployment Test).

💻Demo

Before we begin the demo, I want to explain the tasks we’ll be performing. We will be operating in the LA-Mexico City2 region and we will start with CodeArts Repo to upload our code. Next, we will upload our Dockerfile into CodeArts Artifact. Finally, we will proceed to CodeArts Deploy to deploy our container through an ECS instance we’ve previously created. The container we are deploying is based on an Nginx image, and I will be adding some files from CodeArts Repo to it. You can see the details below when I share the Dockerfile with you.

- Prerequisites for this demo:

1️⃣ECS: We need a server on which to deploy our container. To accomplish this, we require an ECS instance. If you’re unsure about how to purchase an instance, you can refer to this link. In this demo, I’ll use Ubuntu as the operating system with free package configurations.

2️⃣CodeArts Subscription: To access CodeArts services, you will need a subscription. However, there’s no need to worry about payment because we will be using the Basic Version, which is entirely free for up to 10 users!

CodeArts Subscription

3️⃣SWR Organization: We require an organization on SWR to push our images, which you can think of as an Image Repository. You can create it from the SWR Dashboard by simply clicking on ‘Create Organization,’ as shown in the screenshot below👇

Creating an Organization-1

Here, you give the organization a name, and then click ‘OK.’ After that, the organization is created.

Creating an Organization-2

4️⃣Source Code and Dockerfile (You can download them here): These are the files that we need to copy into the container when we build the image using the Dockerfile.

Before building our container image, we need to install Docker on our ECS server. This is necessary because I don’t want CodeArts Deploy to install Docker at the start of every deployment, as it would slow down the continuous deployment process. Let’s establish remote access to the ECS and install Docker by executing this command:

apt-get install docker.io

Docker Installation-1

Make sure that Docker is installed using this command:

docker version

It should resemble the screenshot below👇

Docker Installation-2

After configuring our server, let’s proceed to CodeArts and create a project. To do that, click on ‘Create Project’ located at the top right side of the page.

Creating a Project-1

Next, click on ‘Scrum’ since it’s the only template option presented by CodeArts.

Scrum is a management framework that teams use to self-organize and work towards a common goal. It describes a set of meetings, tools, and roles for efficient project delivery. Much like a sports team practicing for a big match, Scrum practices allow teams to self-manage, learn from experience, and adapt to change.

Creating a Project-2

We give the project a name and then proceed by clicking ‘OK.’

Creating a Project-3

Once we’ve created the project, we need to navigate to Settings > General > Basic Resources to create a host cluster. This step will allow us to add the ECS instances we’ve created to the project.

Creating a Host Cluster-1

We should name this cluster ‘cluster1’ and disable the ‘Use Proxy’ option, as we don’t need it at this moment. Then, click on ‘Save’

A proxy server is a system or router that provides a gateway between users and the internet. Therefore, it helps prevent cyber attackers from entering a private network.

Creating a Host Cluster-2

Now we need to import the ECS instances we have created into the cluster. To do that, click on ‘Import ECS’ located at the top right side of the page.

Creating a Host Cluster-3

Then, click on ‘Import’ on the right side of the ECS instance you want to add.

Creating a Host Cluster-4

Afterward, a pop-up will appear, and you should provide the username, password, and SSH port in their respective input fields so that CodeArts can access the ECS. Then, click on ‘OK.’

Creating a Host Cluster-5

As you can see, the ECS has been successfully added, and the automatic verification has passed.

Creating a Host Cluster-6

Now that we’ve added the ECS instances we’ll use in the project, let’s move to CodeArts Repo and create a repository. We will upload the files we need for this project, including HTML, CSS, and JPG files, which will be copied into the Docker image we are going to create. To create a new repository, click on ‘New Repository’

Preparing the Repository-1

Name the repository ‘webProject’ and change the visibility to ‘Public.’ We’ll keep it public to avoid any potential access issues when building the image from the Dockerfile.

Preparing the Repository-2

After creating the repository, we need the HTTPS address of it to clone it to our local PC. You can obtain it by clicking the ‘Clone/Download’ button at the top right side of the page and then selecting ‘Clone with HTTPS’ to access the link.

Preparing the Repository-3

Now, you can clone the repository to your local PC and push the files into it. To do this, I’ll use Git Bash and enter the following commands, as shown in the screenshot below👇 (It may ask you your Huawei Cloud username and password)

git clone {HTTPS address of the repository}

cd webProject/

git add .

git commit -m 'First Commit'

git push origin master

Preparing the Repository-4

As shown in the screenshot below, our files have been successfully uploaded to CodeArts Repo. Using Git Bash is an efficient way to push multiple files. However, if you need to upload only a few files, you can also use Repo UI. The downside is that you can upload only one file at a time, and files are limited to a maximum size of 10 MB.

Preparing the Repository-5

Before we proceed to Deploy, our final stop will be Artifact, where we will upload our Dockerfile for building the container. We will use this file in the building process, and CodeArts requires us to upload it here. To do so, go to Artifact > Release Repos, then click on ‘Create Folder’ and create a folder under the project’s name. In this case, I’ll name it ‘webDeployment’.

Uploading the Dockerfile-1

Now, you can upload your Dockerfile here. It should contain the following lines:

FROM nginx

Run apt update

RUN apt install git -y

Run git clone {HTTPS address of the repository

WORKDIR /webproject

RUN cp -r . /usr/share/nginx/html

Dockerfile is a text document containing all the commands the user requires to call on the command line to assemble an image.

Uploading the Dockerfile-2

The Dockerfile is now uploaded, and as you may have guessed, it uses the Nginx image as the base starting point. After that, it installs Git and then clones and copies the repository to the related folder where we want to make changes.

Uploading the Dockerfile-3

Until now we have

⚡️Installed Docker into ECS

⚡️Created a Project

⚡️Created a Host Cluster and ECS for the Project

⚡️Created a Repository and pushed our files into the Repo

⚡️Uploaded the Dockerfile into Artifact

After completing all of these operations, we have prepared almost everything for the deployment. Now, let’s proceed to Deploy and initiate the process by clicking on ‘Create Application.’

Creating a Deploy Task-1

Here, I’ll name the application ‘web-server-container’ and then click on ‘Next.’

Creating a Deploy Task-2

CodeArts offers various templates, which can be useful for future projects. However, for this tutorial, we will proceed with the blank template and add tasks step by step.

Creating a Deploy Task-3

In the deployment process, you require an environment to deploy your application. To configure the environment for this application, click on ‘Environment Management.’

Creating an Environment-1

Then, click on ‘Create Environment.’

Creating an Environment-2

I’ll name the environment ‘environment’ and select ‘Linux’ since we created the ECS with Ubuntu. After that, click ‘Save’

Creating an Environment-3

Next, we need to import hosts into the environment. Click on ‘Import Host’ to proceed.

Creating an Environment-4

In the pop-up that appears, select the Host Cluster you created earlier and the hosts associated with it. Since we created and added just one ECS, select it and import it.

Creating an Environment-5

We can see that the verification is successful, as shown in the screenshot below👇

Creating an Environment-6

After setting up the environment as well, we can now go back into the ‘Deployment Actions’ section and find the ‘Select Deployment Source’ task on the Add Actions section of the page. Then we click on ‘Add’

Configuring the Deploy Task-1

Next, configure the input fields as shown in the screenshot below. Use the environment you created a few steps ago and select the Software Package, which is the Dockerfile we uploaded to Artifact. You have the option to change the Download Path, but for this exercise, we will use /usr/local. Remember to use this location when building the image.

Configuring the Deploy Task-2

After configuring the ‘Select Deployment Source’ task, you can add another task following it by clicking on the “+” button when the cursor appears within the task.

Configuring the Deploy Task-3

Next, click on the ‘Container Related’ section, and find the ‘Run Docker Command’ task. Add this task to the workflow.

Configuring the Deploy Task-4

After adding the task, you need to configure it. Start by selecting the environment, and then choose the Docker command you intend to use. Deploy provides various commands such as login, build, tag, push, start, and more. For this step, we want to build an image, so select ‘build.’ Since our Dockerfile has been downloaded to /usr/local, paste this path into the ‘Dockerfile Path’ input area. Finally, for the Execution Parameters, type ‘-t webserver’ to give a name to our image.

Configuring the Deploy Task-5

After the build process, before running the container, I want to push the image first to SWR and then to Docker Hub. To achieve this, we need to log in to the respective container registries. There are two ways to do this: using a login task and a shell command task. Let’s connect to SWR using a login task. To do this, we need to add an Image Repository to the project. Click on ‘Create’ to get started.

Configuring the Deploy Task-6

In the pop-up that appears, you need to fill in the fields with your SWR information as per the screenshot.

Creating an Image Repository-1

To obtain the information needed to fill out the form, go to SWR > My Images and then click on ‘Upload Through Client.’

Creating an Image Repository-2

In the pop-up that appears, it provides guidance for the push operation, as shown in the screenshot below. It instructs you to log in to the VM as a root user and then run the temporary login command. Click on the displayed area to obtain the command.

Creating an Image Repository-3

Once you have obtained the command, take note of the username (after -u), password (after -p), and the repository address (which is at the end).

Creating an Image Repository-4

After acquiring the required information, return to Deploy and fill out the form. Then, click on ‘OK.’

Creating an Image Repository-5

Once logged into the image registry, you need to tag the image to prepare it for pushing into SWR. The format for the image should be like this: {Repository address}/{Organization name}/{Name of the image}

Configuring the Deploy Task-7

After tagging the image, you can now push it into SWR. If you don’t need to add any additional parameters, simply type the tagged image name into the ‘Execution Parameters,’ and it will work as expected.

Configuring the Deploy Task-8

Now, after configuring the push task for SWR, we will log in to Docker Hub and push our image there. This time, we will use shell commands instead of a login task. First, we need to log out from SWR using the

docker logout

command, so we can log in to Docker Hub. After that, we can log in using the

docker login -u {username} -p {password}

command, as shown in the screenshot below👇

Configuring the Deploy Task-9

After completing the login, we should tag the image in the correct format, similar to the steps we followed when pushing to SWR. I’ll configure the tag as shown below and then proceed.

Configuring the Deploy Task-10

Now that we’ve tagged the image, we can push it into Docker Hub using the image name, similar to what’s shown in the screenshot below👇

Configuring the Deploy Task-11

We’ve come a long way, and now we’re ready to run the container! Remember that when deploying a container on CodeArts Deploy, you must include ‘-d’ as a parameter; otherwise, it may fail. In this exercise, we will use the following line:

-d --name ertugrul-website -p 80:80 webserver

as I name it ‘ertugrul-website’ and published it on Port 80. Now that we have configured all the steps and tasks, we are ready to deploy the container on our ECS host server. Click on ‘Save & Deploy’ at the top right side of the page, and the deployment process will commence.

Configuring the Deploy Task-12

CodeArts Deploy provides you with the opportunity to observe real-time logs. As you can see in the screenshot below, our application has been deployed successfully. This means that in this task, we built an image, pushed it into two image registries, and then deployed it as a container.

Deploy Task Logs

We can see that we have successfully pushed the ‘webserver’ image to SWR under the organization named ‘ertbasar.’

SWR Repository

The same applies to Docker Hub. We have successfully pushed the ‘webserver’ image under the repository named ‘ertbasar,’ which is my public repository.

Docker Hub Repository

Furthermore, if we have remote access to the ECS, we can clearly see that our container is up and running!

ECS Container List

You can also connect directly to the server’s Elastic IP (EIP) and observe if the container is running as desired. In the step where we pushed to the repository, we copied the files that I prepared quickly before this demo, which is why we see this page.

Web Server

👉Conclusion

In this article, we have successfully deployed a container on an ECS. In the process, we installed Docker, created a repository, pushed our files, uploaded a Dockerfile, and created a Deploy task. Throughout these steps, we utilized services like ECS, SWR, and CodeArts. This comprehensive demo can serve as a valuable guide for building, organizing, and deploying your images, as all of the processes have been carried out in Huawei Cloud.

💁References

--

--