This article Alibaba Cloud Encyclopedia shares how to deploy and use Docker on cloud service ECS instances. Docker is an open source application container engine with the advantages of portability, scalability, high security and manageability. Developers can package applications and dependencies into a portable container, quickly publish to Linux machines and implement virtualization to build, deploy and manage applications more efficiently. At the same time, Alibaba Cloud provides Docker image sources to facilitate you to quickly deploy Docker.
Table of contents
Install and use docker-compose
Deploy applications using docker-compose
Prerequisites
A basic ECS instance has been created and meets the following configurations.
- Operating system: CentOS 7.x 64-bit, CentOS 8.x 64-bit, Alibaba Cloud Linux 3 64-bit, Alibaba Cloud Linux 2 64-bit
- ECS cloud server: aliyunbaike.com/go/ecs
- Network type: Private network VPC
- IP address: public IP
- Security group: Open ports 80, 22, and 8080 in the inbound direction. For specific operations, see Adding Security Group Rules .
Deploy Docker
- Connect to the ECS instance remotely.
For an introduction to connection methods, see Overview of Connection Methods .
-
Install Docker, CentOS 8.x
-
Switch CentOS 8 source address.
The CentOS 8 operating system version has ended its life cycle (EOL). According to community rules, the content of the source address http://mirror.centos.org/centos/8/ of CentOS 8 has been removed. You can continue to use the default configuration on Alibaba Cloud. The CentOS 8 source error will occur. If you need to use some installation packages in the CentOS 8 system, you need to manually switch the source address.
-
Run the following command to install DNF.
sudo yum -y install dnf
-
Run the following command to install the dependency packages of the Docker storage driver.
sudo dnf install -y device-mapper-persistent-data lvm2
-
Run the following command to add stable Docker software sources.
sudo dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
-
Run the following command to check whether the Docker software source has been added.
sudo dnf list docker-ce
The echo shown in the figure below appears, indicating that the Docker software source has been added. You can use the Alibaba Cloud speed test tool aliyunping.com to test the ping value network delay from the local node to the Alibaba Cloud server in various regions.
-
Run the following command to install Docker.
sudo dnf install -y docker-ce --nobest
-
Execute the following command to check whether Docker is installed successfully.
sudo docker -v
As shown in the following figure, Docker has been installed successfully.
-
Execute the following command to start the Docker service and set it to start automatically at boot.
sudo systemctl start docker sudo systemctl enable docker
-
Execute the following command to check whether Docker is started.
sudo systemctl status docker
As shown in the following echo, Docker has been started.
Docker basic usage
Only the basic usage of Docker is listed below.
-
Managing the Docker daemon
sudo systemctl start docker #运行Docker守护进程 sudo systemctl stop docker #停止Docker守护进程 sudo systemctl restart docker #重启Docker守护进程 sudo systemctl enable docker #设置Docker开机自启动 sudo systemctl status docker #查看Docker的运行状态
-
Manage images
This article takes the Apache image in the Alibaba Cloud warehouse as an example to introduce how to use Docker to manage the image.
-
Pull the image.
sudo docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
-
Modify labels. If the image name is long, you can modify the image label to remember it.
sudo docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1
-
View existing images.
sudo docker images
-
Forcefully delete the image.
sudo docker rmi -f registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
-
-
Manage containers
The <Image ID> below can
docker images
be queried through the command.-
Start a new container.
sudo docker run -it <镜像ID> /bin/bash
-
Start a new container, let the container run in the background, and specify the container name.
sudo docker run -d --name <容器名> <镜像ID>
-
View the container ID.
sudo docker ps
-
Make the container into an image.
sudo docker commit <容器ID或容器名> <仓库名>:<标签>
-
Use Docker to create images
This step guides how to customize a simple Nginx image through Dockerfile.
-
Execute the following command to pull the image. This example uses pulling the Apache image from the Alibaba Cloud warehouse as an example.
sudo docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
-
Modify the image name label to make it easier to remember.
sudo docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1
-
Execute the following command to create and edit the Dockerfile file.
-
Execute the following command to create and edit the Dockerfile file.
vim Dockerfile
-
Press
i
to enter edit mode and add the following content to transform the original image.#声明基础镜像来源。 FROM aliweb:v1 #声明镜像拥有者。 MAINTAINER DTSTACK #RUN后面接容器运行前需要执行的命令,由于Dockerfile文件不能超过127行,因此当命令较多时建议写到脚本中执行。 RUN mkdir /dtstact #开机启动命令,此处最后一个命令需要是可在前台持续执行的命令,否则容器后台运行时会因为命令执行完而退出。 ENTRYPOINT ping www.aliyun.com
-
Press keys
Esc
, enter:wq
andEnter
press keys to save and exit the Dockerfile.
-
-
Execute the following command to build a new image based on the basic image nginx.
The command format is
docker build -t <镜像名称>:<镜像版本> .
, the end of the command.
represents the path of the Dockerfile file and cannot be ignored. Taking building a new image aliweb:v2 as an example, the command is:sudo docker build -t aliweb:v2 .
-
Execute the following command to check whether the new image is built successfully.
sudo docker images
As shown in the following echo, the build is successful.
Install and use docker-compose
docker-compose is an open source container orchestration tool officially provided by Docker for defining and running multiple Docker containers. You can use YAML files to configure all services required by the application, and then use docker-compose to run the command to parse the YAML file configuration, create and Start all Docker services in the configuration file, which has the advantages of low operation and maintenance costs and high deployment efficiency.
important
Only Python 3 and above support docker-compose, and please make sure pip is installed.
Install docker-compose
-
Run the following command to install setuptools.
pip3 install -U pip setuptools
-
Run the following command to install docker-compose.
pip3 install docker-compose
-
Run the following command to verify whether docker-compose is installed successfully.
docker-compose --version
If the echo returns docker-compose version information, it means that docker-compose has been installed successfully.
Deploy applications using docker-compose
The following takes deploying WordPress as an example to introduce how to use docker-compose to deploy applications.
-
Create and edit the docker-compose.yaml file.
-
Run the following command to create the docker-compose.yaml file.
vim docker-compose.yaml
-
Press
i
the key to enter editing mode and add the following content.This example takes installing WordPress as an example.
version: '3.1' # 版本信息 services: wordpress: # 服务名称 image: wordpress # 镜像名称 restart: always # docker启动,当前容器必启动 ports: - 80:80 # 映射端口 environment: # 编写环境 WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: 123456 WORDPRESS_DB_NAME: wordpress volumes: # 映射数据卷 - wordpress:/var/www/html db: # 服务名称 image: mysql:5.7 # 镜像名称 restart: always # docker启动,当前容器必启动 ports: - 3306:3306 # 映射端口 environment: # 环境变量 MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: 123456 MYSQL_RANDOM_ROOT_PASSWORD: '1' volumes: # 卷挂载路径 - db:/var/lib/mysql volumes: wordpress: db:
-
Press
Esc
the key to exit edit mode, then enter to:wq
save and exit.
-
-
Execute the following command to start the application.
sudo env "PATH=$PATH" docker-compose up -d
-
Enter it in the browser
https://云服务器ECS实例的公网IP
to enter the WordPress configuration page. You can configure relevant parameters according to the interface prompts and then access WordPress.
This article is reproduced from Alibaba Cloud official documentation.