Scene introduction
The tutorial introduces how to quickly set up a Docker environment and deploy an Nginx service using Docker.
background knowledge
Container technology
Container is a lightweight, operating system-level virtualization technology that allows us to run applications and their dependencies in the process of resource isolation. All necessary components required to run applications are packaged into a single image. This image It can be reused. When the image is running, it runs in an independent environment and does not share the memory, CPU, or disk of the host operating system with other applications. This ensures that the processes inside the container will not affect any processes outside the container.
Docker
Docker is an open source application container engine that allows developers to package their applications and dependent packages into a portable container, and then publish it to any popular Linux machine or Windows machine. It can also be virtualized. The container is completely Using the sandbox mechanism, there will be no interfaces between each other. The two technologies of Linux cgroup and namespace used at the bottom of Docker implement application isolation. A complete Docker consists of the following parts:
Docker Client client
Docker Daemon daemon
Docker Image image
Docker Container container
Step 1: Connect to the ECS server
Aliyun cloud product resource experience address: https://developer.aliyun.com/adc/scenario/9fd79b8711984e309f20d82bc65a26fa
The scene will provide an ECS instance (cloud server) configured with CentOS 7.7. Through the operations in this tutorial, you can quickly build a Docker environment based on an ECS instance and deploy an Nginx service in the Docker environment.
Step 2: Install Docker CE
There are two branch versions of Docker: Docker CE and Docker EE, the community edition and the enterprise edition. This tutorial is based on CentOS 7 to install Docker CE.
1. Install Docker's dependent libraries.
yum install -y yum-utils device-mapper-persistent-data lvm2
2. Add the software source information of Docker CE.
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3. Install Docker CE.
yum makecache fast
yum -y install docker-ce
4. Start the Docker service.
systemctl start docker
Step 3: Configure Alibaba Cloud Image Warehouse (Image Acceleration)
The default official remote warehouse of Docker is hub.docker.com . Due to network reasons, it may take a long time to download an official Docker image, and the download may even fail. To this end, Aliyun container mirroring service ACR provides an official mirror site to speed up the download of official mirrors. The following describes how to use Alibaba Cloud Mirror Warehouse.
1. Log in to the container mirroring service console.
a. In the resource bar on the left side of the page, click the one-click copy login url, open the browser incognito window (incognito mode) and enter the copied login link. (If you use the opened ECS account, use the ECS account to log in to the console)
b. Enter the sub-user name and sub-user password provided by the resource, and click [Login]; then search for container mirroring, and click [Container Image Service] to log in to the console.
c. The login success page is as follows. (If the service opening window pops up, just close it)
2. Click [Mirror Center]> [Mirror Accelerator], you can see that Alibaba Cloud provides you with a dedicated mirror acceleration address.
- Configure Docker's custom mirror warehouse address. Please replace the mirror warehouse address https://kqh8****.mirror.aliyuncs.com in the command below with the exclusive mirror acceleration address provided by Alibaba Cloud.
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://kqh8****.mirror.aliyuncs.com"]
}
EOF
4. Reload the service configuration file.
systemctl daemon-reload
5. Restart the Docker service.
systemctl restart docker
Step 4: Use Docker to install Nginx service
1. Check the available version of Nginx in the Docker mirror warehouse.
docker search nginx
The command output is as follows:
2. Pull the latest version of Nginx mirror.
docker pull nginx:latest
The command output is as follows:
3. View the local mirror.
docker images
The command output is as follows:
4. Run the container.
docker run --name nginx-test -p 8080:80 -d nginx
Command parameter description:
- --name nginx-test: container name.
- -p 8080:80: port mapping, mapping the local 8080 port to the 80 port inside the container.
- -d nginx: Set the container to always run in the background.
The command output is as follows:
5. Enter http://<ECS public network address>:8080 in the browser address bar to access the Nginx service.