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
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.
.
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](https://labex.io/upload/D/V/E/lDoB6W2UJegC.png)
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](https://labex.io/upload/H/U/J/4cqqVcc2DZXh.png)
Go to the logon page of Alibaba Cloud console.
![image desc](https://labex.io/upload/W/W/D/r1FGF1dU4Hmp.png)
Fill in the sub-user account and click Next.
![image desc](https://labex.io/upload/U/H/M/FWna8Tewo7ua.png)
Fill in the sub-user password and click Log on.
![image desc](https://labex.io/upload/N/J/J/CZpKoFKJnCQL.png)
After you successfully log on to the console, the following page is displayed.
![image desc](https://labex.io/upload/X/X/F/4VQYFXNomWhb.jpg)
3. Install the Docker environment
3.1 Log on to ECS
Click Elastic Computer Service, as shown in the following picture.
![image desc](https://labex.io/upload/S/W/M/K9gFnjId9hAj.jpg)
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](https://labex.io/upload/T/T/F/A3njSoRIIb5j.jpg)
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](https://labex.io/upload/N/O/F/hHLuAqbJ1OCx.jpg)
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](https://labex.io/upload/W/U/P/wJzjeGqmDpNE.png)
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](https://labex.io/upload/F/Y/T/5FJv7FRWHEOD.png)
Run the following command to update the apt installation source:
apt update
![image desc](https://labex.io/upload/U/A/U/9iCPjBl0VY6x.png)
Run the following command to install related tools:
apt -y install apt-transport-https ca-certificates curl software-properties-common
![image desc](https://labex.io/upload/J/K/K/PHs9hiTYcpgW.png)
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](https://labex.io/upload/K/B/H/Z9XwvB5s7xmt.png)
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](https://labex.io/upload/Y/U/N/3nkwwM4cyZm8.png)
Run the following command to update the apt installation source again:
apt update
![image desc](https://labex.io/upload/F/H/I/yFMgzM1rmG7Q.jpg)
Run the following command to install Docker:
apt -y install docker-ce
![image desc](https://labex.io/upload/X/H/J/hjAiaQcSlzCN.png)
Run the following command to view the Docker version:
docker version
![image desc](https://labex.io/upload/E/V/X/1LXxLAeDu1CN.png)
Run the following command to view the Docker running state:
service docker status
![image desc](https://labex.io/upload/Y/R/R/pTxQjsZrTf4y.png)
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](https://labex.io/upload/L/L/P/cHZAT5wcFP10.png)
Run the following command to view the Ubuntu image:
docker images
![image desc](https://labex.io/upload/H/X/T/B7q4fSzJ9iWX.png)
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](https://labex.io/upload/S/A/B/Fgp0qqDoTp4R.png)
Run the following command to update the apt installation source:
apt update
![image desc](https://labex.io/upload/P/U/G/xsFPv3WaVqzr.png)
Run the following command to install the Nginx service:
apt -y install nginx
![image desc](https://labex.io/upload/D/W/O/ZiRP6nD4hLfC.png)
Run the following command to install the lsof command, which is used to view Nginx ports:
apt -y install lsof
![image desc](https://labex.io/upload/B/K/I/gJ1AmIulxAG5.png)
Run the following commands to start the Nginx service and view an Nginx port:
nginx
lsof -i:80
![image desc](https://labex.io/upload/C/S/W/A0ZapMQrOArq.png)
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](https://labex.io/upload/N/Y/G/7g62nF55dyOy.png)
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](https://labex.io/upload/R/C/I/LS9cTSDnd1Hn.png)
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](https://labex.io/upload/E/A/C/Aexqvfn0IUz7.png)
The Nginx service of the Docker container has been installed.
Run the following command to exit the current container:
exit
![image desc](https://labex.io/upload/Y/P/V/sQO54BY2lZix.png)
The custom container has been modified.
Run the following command to view container information:
docker ps -a
![image desc](https://labex.io/upload/V/V/A/63EMRJV3N7N6.png)
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](https://labex.io/upload/R/F/S/uYBtMHFaFGfF.png)
The image has been created.
Run the following command to view the image:
docker images
![image desc](https://labex.io/upload/X/Q/F/nwp35mj2iJn7.png)
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](https://labex.io/upload/B/Q/D/Q1Nqgmx6p09F.png)
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](https://labex.io/upload/C/E/U/JBVpqt6kcDU7.png)
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](https://labex.io/upload/X/D/Q/w5bbqkRAHu09.png)
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](https://labex.io/upload/G/V/Q/gHWgqn5joK8o.png)
Run the following command to view the image:
docker images
![image desc](https://labex.io/upload/K/P/V/q4TnUo2ZKQxO.png)
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](https://labex.io/upload/T/N/U/xRDJ2aat3ci9.png)
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](https://labex.io/upload/H/B/R/e0vULOmvOTq3.png)
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](https://labex.io/upload/J/Y/N/zupvVS0uWXt4.jpg)
Select US(Silicon Valley)
![image desc](https://labex.io/upload/R/T/I/OwWhkzkBhW04.jpg)
![image desc](https://labex.io/upload/Y/H/P/NIbn5TuqnmiS.jpg)
![image desc](https://labex.io/upload/A/X/L/f2aqh9beYNOF.jpg)
The prompt shown in the following figure appears upon your first logon.
![image desc](https://labex.io/upload/L/O/I/jP6Immkmg5cU.jpg)
Set the Docker logon password to Aliyun-test.
![image desc](https://labex.io/upload/G/C/N/1NjIGb99QNTF.jpg)
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](https://labex.io/upload/V/O/H/2uDWSAu4gnSJ.jpg)
The following figure shows that the namespace has been created.
![image desc](https://labex.io/upload/P/Y/Q/eYp52JP7GGoT.jpg)
Create a repository according to the following figure. Set the region to US(Silicon Valley).
![image desc](https://labex.io/upload/W/A/U/jCY2u8TojAYC.jpg)
Set parameters according to the following figure and click Next. Select the namespace you created earlier.
![image desc](https://labex.io/upload/U/K/A/40whhSx2THIz.jpg)
Select Local Repository and click Create Repository.
![image desc](https://labex.io/upload/M/Q/S/GsCJHOUJBnGT.jpg)
The following figure shows that the namespace has been created.
![image desc](https://labex.io/upload/X/P/I/GxfawTAjIlCj.jpg)
Click Manage to open the repository.
Detailed commands for pushing images to this repository are displayed.
![image desc](https://labex.io/upload/L/G/Y/BeM1Ooazt7HF.jpg)
Copy the first command shown in the following figure to the ECS terminal and enter the repository logon password.
![image desc](https://labex.io/upload/W/W/I/TO2Bp151D9Vk.png)
Run the following command to obtain the ID of nginx-build-image:
docker images
![image desc](https://labex.io/upload/K/Q/M/VoKkaEBJx2d0.png)
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](https://labex.io/upload/J/O/R/rMiDdAvL4LB7.jpg)
![image desc](https://labex.io/upload/F/W/L/KtrnnOlfYusD.png)
Copy the third command shown in the following figure to the ECS terminal (set [tag] to v1).
![image desc](https://labex.io/upload/C/U/I/11im2XxHU4fr.jpg)
The following figure shows that the image is being uploaded.
![image desc](https://labex.io/upload/R/A/B/eQcbgn96ZgfY.png)
The following figure shows that the image has been uploaded.
![image desc](https://labex.io/upload/J/L/K/V8Q5pIN7Y8Iv.png)
Go to the Alibaba Cloud console and select Tags. The uploaded image is displayed.
![image desc](https://labex.io/upload/T/J/P/p6bSHTdIIFhn.jpg)
For details about how to download the image in other environments, see the repository guide.
![image desc](https://labex.io/upload/N/D/V/afm2BNv4cu3m.jpg)
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](https://labex.io/upload/K/M/E/A10M9UfbMbbp.jpg)
Click Delete and OK.
![image desc](https://labex.io/upload/G/A/K/n8sz7ddqK6az.jpg)
Delete the namespace according to the following figure.
![image desc](https://labex.io/upload/S/A/A/QKRji1NxZWG4.jpg)
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](https://labex.io/upload/V/A/I/B3nlg5AxTwXL.png)
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.