☁️ Deploy a Python Application on a CodeArts CI/CD Pipeline by Using Docker Containers and SWR

Hakanaktas
Huawei Developers
Published in
7 min readAug 8, 2024
Huawei Cloud Codearts with Docker Container

Introduction

Continuous Integration (CI) and Continuous Deployment (CD) are foundational elements in modern software development, particularly when integrated with Docker containerization. This article delves into how CI/CD can streamline the development process by ensuring consistent and automated testing and deployment of Docker containers.

The primary goal of integrating CI/CD with Docker is to eliminate the inconsistencies often encountered in development environments through automation. By implementing CI/CD, developers can automatically build, test, and deploy their applications within Docker containers across different stages of the development lifecycle

Cloud CI CD pipeline

We will discuss the setup of a CI/CD pipeline that facilitates the deployment of a containerized web application, highlighting how these practices reduce errors, save time, and enhance product quality by enabling frequent and reliable updates.

Step-1: Creating Project:

  1. Firstly, create a project by selecting a scrum template.
  2. Then, enter a name and click ‘OK’.

Basic information:

  • Work Template: Scrum (System)
  • Name: PythonFlaskAppDocker
  • Others: Default
Creating Project

Step-2: Creating Repository:

  1. After creating a project, go to the Code > Repo page from the left side menu.
  2. Click on “New Repository” to create a repository. There are 3 repository types Common, Template, and Import.
  3. Select the repository type as an “Import” to pull the repository from GitHub and click “Next”.
  4. Select “Git URL” in the Import From Section. To import the source code enter the Source Repository URL of the GitHub repository.
  5. Give detailed information about what will be created in the repository.

Basic information:

  • Repository Type: Import (Import a repository from Git URL, SVN, GitHub, and GitLab)
  • Import From: Git URL
  • Source Repository URL: https://github.com/hakanaktas1/codearts-python-flask.git
  • Visibility: Public
  • Repository Name: codearts-python-flask
  • Initial Settings: Enable
  • Visibility: Public
  • Branch: Default branch
  • Schedule: Enable
Creating Repository

Step-3: Creating SWR Organization

  1. Go to the Huawei Cloud Console and open the SWR (Software Repository for Container) service that supports all lifecycle management of container images.
  2. Click on “Create Organization” on the top right and enter a name for the organization. Then, click “OK”.

Basic information:

Organization name: flask

Step-4: Creating Building Actions

  1. After creating the repository, go to CICD > Build page. Click “Create Task” to create a building task.
  2. Enter the basic information such as project name, code source, and repository, and click “Next”.
  3. In the CodeArts Build service, there are several templates to provide ease of use for developers. In this exercise, “Blank Template” will be selected.
  4. After opening the blank template on the left side GUI click “Add Action” and select “Build Image and Push to SWR”.
  5. Enter the detailed information about the SWR repository.

Basic information:

  • Tool Version: docker 18.03
  • Image Repository: SWR
  • Authorized User: Current
  • Organization: flask
  • Image Name: flaskapp
  • Image Tag: v1.1
  • Working Directory: .
  • Docker File Path: ./Dockerfile
  • Add Build Metadata to Image: No
Creating Building Actions

After finishing the building, you can check the progress on the left side menu and also, from by building logs. If there are any errors it shows and gives solution suggestions.

Building Logs

Note: After the building process, go to the SWR > My Images and check the docker image like below. Click the image name and edit. Then, change its type to Public.

SWR>My Images: List of docker images

Step-4: Creating Host Cluster on Basic Resources

  1. Before moving to the deploy actions, you must create a host cluster and add the target host that computing instances for running the application.
  2. Go to Settings> General > Basic Resources and click “Create Host Cluster”
  3. Enter the basic information such as “Cluster Name”, “OS”, “Host Connection Mode”, etc.
  4. On the “Target Host” section, click “Add Host” Select your host from the list shown and import it.
  5. Enter detailed information about the host such as “Host Name”, “Authorization”, “Username”, “Password” and “SSH Port”.
  6. Click “OK” to add and you should see the cluster below.

Basic information:

Cluster Name: flaskApp

OS: Linux

Host Connection Mode: Direct Connection

Execution Resource Pool: Official

Host Name: ecs-flask

Authorization: Password

Username: root

SSH Port: 22

Note: Make sure that port 22 for SSH connection and 8080 for Web Application are open in the security group to which the host belongs.

Adding Host as a Resource into the project

Step-5: Creating Deploy Actions

  1. Go to the CICD > Deploy page on the left side menu. Then, click “Create Application”.
  2. Enter the basic information about the project such as “Name”, “Project”, etc.
  3. Select “Deploy a Docker Application for Linux” to deploy the project by using Docker.
  4. Delete the actions from the template such as “Log in to a Docker Image Repository”, “Select a Docker File” and, “Build Image”. Because we already push the image on the building section.
  5. Go to the “Environment Management” section on the top menu, and click “Create Environment”.
  6. Enter basic information about the environment such as “Environment Name”, “Resource Type” and “OS”.
  7. On the “Resources” page, select the host that you create in Step 4.
  8. Click “Import” and the virtual machine on which the deployment process will be performed has been selected.
  9. Go back to the “Deployment Actions”, copy the following command, and paste it into “Run Shell Commands”. Enter the given basic information below. The template should be like the shown image below.
sudo apt-get install libssl-dev libffi-dev python-dev build-essential libxml2-dev
pip3 install -i https://pypi.org/simple six --user -U
pip3 install -i https://pypi.org/simple docker-compose==1.17.1

sudo systemctl start docker

Basic information:

Install Docker > Action Name: Default

Environment: env

Operation: Install Docker

Docker Version: 18.03

Run Shell Commands > Environment: env

Pull Images > Environment: env

Pull Images > Command: pull

Pull Images > Execution Parameters: swr.ap-southeast-3.myhuaweicloud.com/flask/flaskapp:v1.1

Create and Start Container > Environment: env

Create and Start Container > Command: run

Create and Start Container > Execution Parameters: -it -d -p 8080:8080 swr.ap-southeast-3.myhuaweicloud.com/flask/flaskapp:v1.1

Deploying Actions

Step-6: Creating Pipeline

  1. Go to the CICD > Pipeline and click “Create Pipeline”
  2. Enter the basic information as given below.
  3. Select the “Python-Build” template and click “OK”.
  4. After you create a template, click “Build-Task” enter the information, and select the build task that you created in Step 3.
  5. Click on “ + Stage” to add the deploy stage. Then, click on “+ Job” to add a Deploy extension.
  6. Select the deploy task you created on step-5 and click “OK”.
  7. Finally, the pipeline is ready! Click “Execute”
Creating a Pipeline

Step-7: Accessing the Web Application on the Browser

  1. Go to the “Elastic Cloud Server” service and copy the Elastic IP of the host to which you deploy the application.
  2. Paste it on the browser, the URL should be like below.
  3. If you can view the web page like the one below, congratulations.
Basic Python Flask Web Application Screen Shot-1
Basic Python Flask Web Application Screen Shot-2

Conclusion

Deploying a Python application on a CodeArts CI/CD pipeline using Docker containers and SWR (Software Repository for Container Images) provides a modern, scalable, and efficient solution for managing application lifecycles. This approach ensures that your Python application runs consistently across different environments, thanks to the containerization provided by Docker. By integrating with Huawei Cloud’s SWR, you gain the ability to securely store, manage, and deploy container images, simplifying the deployment process.

The combination of Docker and SWR within CodeArts CI/CD pipelines not only automates the build and deployment processes but also enhances security and version control. Each deployment is versioned and can be rolled back if necessary, minimizing downtime and risks associated with releases. Moreover, using containers allows for microservices architecture and better resource utilization, which are key advantages in today’s cloud-native application development.

By adopting this approach, development teams can accelerate their deployment cycles, ensure consistency between development and production environments, and maintain a high level of agility in responding to changes. Ultimately, deploying a Python application using Docker and SWR in a CodeArts CI/CD pipeline positions your project for success in a competitive and rapidly evolving tech landscape.

References

--

--