Dockerize App and Push to Container Registry: CI/CD Automation on Container Service (1)

By Evan Wong, Solutions Architect

1. Prerequisites

Before going through the step-by-step guides, the user should have the following prerequisites:

  • A web browser, recommended Google Chrome.
  • A stable internet connection.
  • An Alibaba Cloud account.
  • Basic knowledge of operations of Linux operating systems.
  • A good understanding and basic knowledge of Docker container and the usage of Dockerfile.
  • A fundamental knowledge of Kubernetes.
  • A GitHub account.

2. Resources

This tutorial uses a number of third party resources including the sample application source codes. Special thanks to Satya Depareddy for the application source codes on GitHub —
https://github.com/depareddy/java-webapp-docker

3. Introduction

This document provides a fundamental DevOps best practices guide on Alibaba Cloud. In this guide, you will understand the best practices on how to implement the continuous integration and continuous deployment (CI/CD) on using the cloud services on Alibaba Cloud.

4. Scenario

A financial institution that have deployed a customer facing website portal that allows investor to view their portfolio, invest new fund, purchase additional funds, view the funds’ performance and statistics. At the moment, the customer is using the ECS, SLB and Auto Scaling to host their application workloads. The customer is using traditional way to do deployment, once the developer changes the source code from the source code repository, it would continue for unit testing. After successful testing, the developer would manually package it into customer image and store it on the Cloud. Then, it would be used to create ECS based on the image.

5. Continuous Integration & Continuous Deployment (CI/CD)

5.1 CI/CD Steps

CI/CD process generally follows the following scheme:

  • Checkout the source codes
  • Build and run unit tests
  • Dockerize the application
  • Push dockerized application to Docker Registry
  • Deploy the image to the Kubernetes cluster

6. Create a CI/CD Server

In this section, you would be creating a new server to act as a development/CI server. You would be installing docker on the server, clone a sample application to the server. After that, you would run docker build and package it into a container. At last, you will be running the application on the server.

6.1 Create a Virtual Private Cloud

On the home menu, go to Products -> Networking -> Virtual Private Cloud

6.2 Purchase Elastic Compute Service (ECS)

Go to the Home -> Products -> Elastic Computing -> Elastic Compute Service

6.3 Setup Development/CI Server

Logon to the CI server that was created in the earlier lab. On the PC or laptop, open a terminal or command prompt or Putty. To logon to the ECS, use ssh command.

$ ssh root@x.x.x.x

6.3.1 Uninstall Old Dockers

Older versions of Docker were called docker or docker-engine. If these are installed, uninstall them, along with associated dependencies.

$ sudo yum remove docker docker-common docker-selinux docker-engine

6.3.2 Install Docker CE

Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data and lvm2 are required by the devicemapper storage driver.

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum install docker-ce -y
$ sudo systemctl start docker
$ sudo docker run hello-world

6.4 Build Docker Image

Install the latest version of git.

$ sudo yum install git -y

6.4.1 Clone the source codes to the CI server

Next, you would need to clone the codes to the local computer.

$ git clone https://github.com/echoesian/java-webapp-docker

6.4.2 Docker build

To build the docker, first change to the directory of the source codes that have cloned locally.

$ cd java-webapp-docker
$ docker build -t simplewebapp .

6.4.3 Verify docker image

Verify if the docker image is built successfully.

$ docker images

6.5 Run Docker Image Locally

Before the docker being pushed to the Kubernetest, let’s try to run it locally to make sure everything is running properly.

$ docker run -p 8080:8080 simplewebapp

6.5.1 View the web application on the browser

Open your browser and enter the URL of the web application, for e.g. if the CI server IP address is 47.254.192.185: http://47.254.192.185:8080/simplewebapp/

7. Setup Container Registry

7.1 Create GitHub Account (Optional)

If you do not have a GitHub account, go to www.github.com and sign up for a new account. Fill in the username, email and password. Then, after verification, choose the Free account.

7.2 Fork the Application Source Codes

In this lab, we are using GitHub as the source code repository. First, you would need to fork the source codes from existing Git repository: https://github.com/echoesian/java-webapp-docker. To do this, login into your own GitHub and navigate to this repository https://github.com/echoesian/java-webapp-docker. Click on the Fork on the top right hand corner on the screen.

7.3 Bind GitHub Account to Container Registry

Go to the Alibaba Cloud console, click Home in the upper left corner of the page, and select Container Registry.

7.4 Namespace

Go back to the Namespace page. On the default prompt, click OK.

  • Use team or organization as the namespace: misaka-team

7.5 Create New Repository

Create a repository according to the following figure. Set the region to Malaysia (Kuala Lumpur) or any other region of your choice.

7.6 Push the Docker Image to the Container Registry

On the root directory, change to the directory of the source codes that have cloned locally.

$cd java-webapp-docker

7.6.1 Dockerfile

Open the dockerfile and review the file. Below is the dockerfile, which simply means:

  1. Setup the working directory
  2. Copy the source codes to the target image directory
  3. Run maven build
  4. Download Tomcat image and deploy to the tomcat container
  5. Expose port 8080
  6. Run the Tomcat server
# setup working directory
FROM maven AS build
RUN mkdir /app
WORKDIR /app
# maven build
COPY src /app/src
COPY pom.xml /app
RUN mvn -f /app/pom.xml clean package
# deploy to tomcat server
FROM tomcat
COPY --from=build app/target/simplewebapp.war /usr/local/tomcat/webapps
EXPOSE 8080
CMD ["catalina.sh", "run"]
docker images

7.7 Configure Automatic Image Build

Go to the build section, enable the Automatically build image option.

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com