arrow

Create Custom Docker Images

1. Experiment

1.1 Knowledge points

This experiment involves Docker containers and Alibaba Cloud’s Container Registry service. In this experiment, you will create an Nginx image based on an Ubuntu image in two methods, and then upload the created image to Alibaba Cloud’s image repository. Container Registry provides functions such as application image hosting, image security scanning, and image building. It helps you easily manage images over a full lifecycle.

1.2 Experiment process

  • Install the Docker environment.
  • Create a custom image in two methods.
  • Upload the created image to Alibaba Cloud’s image repository.

1.3 Cloud resources required

  • ECS

1.4 Prerequisites

  • You have a basic understanding of Docker.

2. Start the experiment environment

Click Start Lab in the upper right corner of the page to start the experiment.

image desc.

After the experiment environment is successfully started, the system has deployed resources required by this experiment in the background, including the ECS instance, RDS instance, Server Load Balancer instance, and OSS bucket. An account consisting of the username and password for logging on to the Web console of Alibaba Cloud is also provided.

image desc

After the experiment environment is started and related resources are properly deployed, the experiment starts a countdown. You have an hour to perform experimental operations. After the countdown ends, the experiment stops, and related resources are released. During the experiment, pay attention to the remaining time and arrange your time wisely. Next, use the username and password provided by the system to log on to the Web console of Alibaba Cloud and view related resources:

openCole

Go to the logon page of Alibaba Cloud console.

image desc

Fill in the sub-user account and click Next.

image desc

Fill in the sub-user password and click Log on.

image desc

After you successfully log on to the console, the following page is displayed.

image desc

3. Install the Docker environment

3.1 Log on to ECS

Click Elastic Computer Service, as shown in the following picture.

image desc

We can see one running ECS instance in the US West 1 region. Click it to go to the ECS console as shown in the following picture.

image desc

Copy this ECS instance’s Internet IP address and remotely log on to this ECS (Ubuntu system) instance. For details of remote logon, refer to login

image desc

The default account name and password of the ECS instance:

Account name: root

Password: nkYHG890..

3.2 Install Docker

Enter the following command and view the internal network address of ECS

ifconfig

image desc

The host name and intranet IP address of the ECS are identified in the above figure.

Enter command “vim /etc/hosts” and open the system’s hosts configuration file

Paste the following to the end of the file,Please pay attention to replacing YOUR-PRIVATE-IP and YOUR-HOST-NAME with your own

YOUR-PRIVATE-IP  YOUR-HOST-NAME

image desc

Run the following command to update the apt installation source:

apt update

image desc

Run the following command to install related tools:

apt -y install apt-transport-https ca-certificates curl software-properties-common

image desc

Run the following command to install a GPG certificate:

curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

image desc

Run the following command to add software source information:

add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

image desc

Run the following command to update the apt installation source again:

apt update

image desc

Run the following command to install Docker:

apt -y install docker-ce

image desc

Run the following command to view the Docker version:

docker version

image desc

Run the following command to view the Docker running state:

service docker status

image desc

Docker is running. Press q to exit.

4. Create a custom image

You need to download a basic operating system image before creating a custom image. An Ubuntu image is used as an example.

Run the following command to download an Ubuntu image:

docker pull ubuntu

image desc

Run the following command to view the Ubuntu image:

docker images

image desc

You can create a custom Docker image in two methods: commit and build.

4.1 Commit

Run the following command to use the downloaded Ubuntu image to create a container and open the container:

docker run -p 80:80 -it ubuntu:16.04 /bin/bash

image desc

Run the following command to update the apt installation source:

apt update

image desc

Run the following command to install the Nginx service:

apt -y install nginx

image desc

Run the following command to install the lsof command, which is used to view Nginx ports:

apt -y install lsof

image desc

Run the following commands to start the Nginx service and view an Nginx port:

nginx

lsof -i:80

image desc

Port 80 of the container is in the listening state.

Run the following command to install the vim command, which is used to view text files:

apt -y install vim

image desc

Run the following command to open the default Nginx configuration file:

vim /var/www/html/index.nginx-debian.html

Modify the file content according to the following figure, save the file, and exit.

image desc

Enter the following address in the address bar of a browser (replace YOUR-ECS-IP with the actual IP address):

http://YOUR-ECS-IP

image desc

The Nginx service of the Docker container has been installed.

Run the following command to exit the current container:

exit

image desc

The custom container has been modified.

Run the following command to view container information:

docker ps -a

image desc

Run the following command to turn the container into an image (replace YOUR-CONTAIN-ID with the container ID shown in the preceding figure):

docker commit YOUR-CONTAIN-ID nginx-commit-image:v1

image desc

The image has been created.

Run the following command to view the image:

docker images

image desc

The size of the new image is larger than that of the basic image, because many things have been installed. (The image size shown in the preceding figure is just for reference.)

4.2 Build

Use the Dockerfile configuration file to create an image.

Run the following command to create a demo directory and go to the directory:

mkdir demo && cd demo

image desc

Run the vim index.nginx-debian.html command to create an index.nginx-debian.html file, copy the following content to this file, save the file, and exit.

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to labex custom nginx!</h1>
</body>
</html>

image desc

Run the vim Dockerfile command to create a Dockerfile file, copy the following content to this file, save the file, and exit.

FROM ubuntu

MAINTAINER labex@aliyun.com

RUN apt update && \
    apt install -y nginx && \
    apt install -y vim && \
    apt install -y lsof

WORKDIR /var/www/html

ADD index.nginx-debian.html /var/www/html

#ENV PATH /usr/local/nginx/sbin:$PATH

EXPOSE 80

CMD /bin/sh -c 'nginx -g "daemon off;"'

image desc

Dockerfile is the configuration file used to create a Docker image, and the instructions in the file specify the steps for creating the image. For details about the syntax, see the official Docker documents. The following describes the instructions used in the Dockerfile file:

FROM: specifies the basic image. This instruction must be placed on the first line.
MAINTAINER (optional): specifies the image maintainer.
RUN: runs commands in the container. If multiple commands exist, the commands will be run in sequence.
WORKDIR: specifies the default directory when you open the container.
ADD: copies the files from the host to the container.
ENV: sets environment variables.
EXPOSE: specifies external ports of the container.
CMD: specifies the command to be run by default when the container is started.

Run the following command to create an image named nginx-build-image:

docker build -t nginx-build-image:v1 .

image desc

Run the following command to view the image:

docker images

image desc

Run the following command to use nginx-build-image to create a container:

docker run -p 80:80 -d nginx-build-image:v1

image desc

The container is started.

Enter the following address in the address bar of a browser (replace YOUR-ECS-IP with the actual IP address):

http://YOUR-ECS-IP

image desc

The second method is more convenient for creating an image. The Dockerfile file records the steps used to create the image, which makes subsequent upgrade and maintenance easier. The second method is widely used in the production environment.

5. Upload the created image to the image repository

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

image desc

Select US(Silicon Valley)

image desc

image desc

image desc

The prompt shown in the following figure appears upon your first logon.

image desc

Set the Docker logon password to Aliyun-test.

image desc

Create a namespace according to the following figure. The new namespace cannot be the same as an existing one. If the namespace you entered already exists, enter another one.

image desc

The following figure shows that the namespace has been created.

image desc

Create a repository according to the following figure. Set the region to US(Silicon Valley).

image desc

Set parameters according to the following figure and click Next. Select the namespace you created earlier.

image desc

Select Local Repository and click Create Repository.

image desc

The following figure shows that the namespace has been created.

image desc

Click Manage to open the repository.

Detailed commands for pushing images to this repository are displayed.

image desc

Copy the first command shown in the following figure to the ECS terminal and enter the repository logon password.

image desc

Run the following command to obtain the ID of nginx-build-image:

docker images

image desc

Copy the second command shown in the following figure to the ECS terminal (replace [ImageId] with the actual one and set [tag] to v1).

image desc

image desc

Copy the third command shown in the following figure to the ECS terminal (set [tag] to v1).

image desc

The following figure shows that the image is being uploaded.

image desc

The following figure shows that the image has been uploaded.

image desc

Go to the Alibaba Cloud console and select Tags. The uploaded image is displayed.

image desc

For details about how to download the image in other environments, see the repository guide.

image desc

The following describes how to delete a repository.

Click the button in the upper left corner to go back to the repository page.

image desc

Click Delete and OK.

image desc

Delete the namespace according to the following figure.

image desc

Reminder:
Before you leave this lab, remember to log out your Alibaba RAM account before you click the ‘stop’ button of your lab. Otherwise you’ll encounter some issue when opening a new lab session in the same browser:

image desc image desc

6. Experiment summary

In this experiment, you have used the commit and build methods to create a custom Nginx image based on an Ubuntu Docker image. Then, you have uploaded the image to Alibaba Cloud’s Container Registry service. Container Registry is a free image repository that Alibaba Cloud provides, and you do not need to establish and maintain the environment. Container Registry supports multiple regions and provides stable and quick image uploading and downloading services, significantly reducing maintenance costs and improving image management efficiency.