Let’s say you are a container micro-services developer. You have a lot of container images, each with multiple versions, and all you are looking for is a fast, reliable, secure, and private container registry. You also want to instantly upload and retrieve images, and deploy them as a part of your uninterrupted integration and continuous delivery of services. Well, look no more! This article is for you.
This article introduces you to the Alibaba Cloud Container Registry service and its abundance of features. You can use it to build images in the cloud and deploy them in your Alibaba Cloud Docker cluster or premises. After reading this article, you should be able to deploy your own Alibaba Cloud Container Registry.
What is Alibaba Cloud Container Registry?
Alibaba Cloud Container Registry (ACR) is a scalable server application that builds and stores container images, and enables you to distribute Docker images. With ACR, you have full control over your stored images. ACR has a number of features, including integration with GitHub, Bitbucket, and self-built GitLab. It can also automatically build new images after the compile and test from source code to applications.
In this tutorial, we will build and deploy containerized images using Alibaba Cloud Container Registry.
Step 1: Activating Alibaba Cloud Container Registry
You should have an Alibaba Cloud account set up. If you don’t have one, you can sign up for an account and try over 40 products for free. Read this tutorial to learn more.
The first thing you need to do is to activate the Alibaba Cloud Container Registry. Go to the product page and click on Get it Free.
It will take you to the Container Registry Console where you can configure and deploy the service.
Step 2: Configuring Alibaba Cloud Container Registry
Create a Name Space
A namespace is a collection of repositories and repository is a collection of images. I recommend creating one namespace for each application and one repository for each service image.
After creating a namespace, you can set it up as public read or private in the settings.
Create and upload a Local Repository
A repository (repo) is a collection of images. I suggest you collect all versions of the image of one service in one repository. Click Create Repo and fill out the information in the page. Select Local Repository. After a short while, a new repository will be created, which has its own Repository URL. You can see it on the image list page.
You can now upload your locally built image to this repository.
Step 3: Connecting to Container Registry with Docker Client
In order to connect to any container registry from Docker client, you first need to set Docker login password in the ACR console. You will use this password on your Docker client to login onto the registry.
Next, on the Image List page, click on Admin in front of the repository you want to connect. Here you can find all the necessary information and commands to allow Docker client access the repository. You can see Image Name, Image Type, Internet and intranet addresses of the repository. You can use the internet address to access the repository from anywhere in the world. If you want to use the repository with your Alibaba Cloud container cluster, you should use the internet address because it will be much faster.
Copy the Login, push and pull commands. You will need it later.
Start up the Docker client in your local machine. You can refer to docker.io to install a Docker client on to your computer. On MAC, run the docker.app application to start the Docker client.
Login as user on Docker client.
docker login --email@example.com registry-intl.ap-southeast-1.aliyuncs.com
Note: Replace random_name with the actual username.
You will see a login successful message after your enter the password and hit enter. At this point, you are authenticated and connected to the Alibaba Cloud Container Registry.
Step 4: Building an Image Locally and Pushing to ACR
Let’s write a Dockerfile to build an image. The following is a sample Dockerfile; you can choose to write your own Dockerfie:
# This is the first image for the static site.
#A name can be given to a new build stage by adding AS name to the FROM instruction.
LABEL NAME = static-Nginx-image
START_TIME = 2018.03.10
AUTHOR = "Fouad"
LABEL DESCRIPTION = "This image is built for static site on DOCKER"
LABEL VERSION = 0.0.0
#RUN mkdir -p /var/www/
ADD /public /usr/share/nginx/html/
RUN service nginx restart</code></pre>Run the Docker build command to build the image. In order to later push the image to the repository, you need to tag the new image with the registry: <pre><code>docker build -t registry-intl-internal.ap-southeast-1.aliyuncs.com/fouad-space/ati-image .
Once the build is complete, it will be tagged with repository name already. You can see the new image in by using the command:
Docker image ls
Push the image to ACR repository with the command:
docker push registry-intl.ap-southeast-1.aliyuncs.com/fouad-space/ati-image:latest
To verify that the image is pushed successfully, see it in the Container Registry console. Click on Admin in front of the repository name and then click Image version.
Pull the image and create a container. Run the docker pull command:
docker pull registry-intl.ap-southeast-1.aliyuncs.com/fouad-space/ati-image:latest
Since I have already pulled the image to my local computer, the message says image is up to date.
Create a new container using this image:
docker run -ti -p 80:80 registry-intl.ap-southeast-1.aliyuncs.com/fouad-space/ati-image bash
Step 5: Building an Image Repo with GitHub
With Alibaba Cloud Container Registry, you can build images in the cloud as well as push them directly to the registry. Besides this, Container Repository supports automatically triggering the build when the code changes.
If Automatically create an image when the code changes is selected in Build Settings, the image can be automatically built after you submit the code, without requiring you to manually trigger the build. This saves manual work and keeps the images up-to-date.
Create a GitHub repo and upload your Docker file to the repo.
Then, return to the Container Registry console to create a repo. Select GitHub repo path and complete the repository creation steps.
Once the repository is created, go to Image List and click on Admin on the repo name, click Build, and finally click Build Now.
You can see the build progress in the menu and the complete logs of the build process.
You can also see all the build logs. Isn’t it neat?
Once the build is complete, your image is ready to be deployed. You can pull it to the local Docker engine or deploy this image on Alibaba Cloud Container Service.
Step 6: Creating a Webhook Trigger
Webhook is a type of trigger. If you configure this, it will push a notification when an image is built and therefore set up a continuous integration pipeline.
How does it work? Well, suppose you have set a Container Service trigger for Webhook. When an image is built or rebuilt, the applications in Container Service are automatically triggered to pull the latest image and re-deployed.
To create a webhook, you first need to go to container service and get the application web URL.
Now use this URL to configure a hook. Every time the image in the container registry is updated, this application will be re-deployed with the new image. Be very careful though, incorrect setup can bring down the whole application. But rollback is possible in the container service so no big worries.
In this article, you should have learned the following:
- What is Alibaba Cloud Container Registry service and how you can implement it.
- How to create a name space and repository to host Docker images.
- How to build a Docker image locally and push it to ACR.
- How to pull a Docker image from ACR and instantiate a new scontainer with it.
- Building an image in Container Registry with GitHub source code.
- How to automatically trigger the pull request for the latest image and re-deploy the service.