This topic describes how to deploy and use Docker on an Elastic Compute Service (ECS) instance.
Background information
Docker is an open-source containerization engine that is portable, extensible, secure, and manageable. Docker allows developers to implement virtualization by packaging applications and dependencies into portable containers and then publishing the containers to Linux machines. With Docker, the creation, deployment, and management of applications can be streamlined. Alibaba Cloud provides Docker image repositories that you can use to quickly deploy Docker.
Prerequisites
An ECS instance that is configured with the following settings is created. If the instance is not created, create the instance. For more information, see Create an instance by using the wizard.
Operating system: CentOS 7.x 64-bit, CentOS 8.x 64-bit, Alibaba Cloud Linux 3 64-bit, or Alibaba Cloud Linux 2 64-bit
Network type: Virtual Private Cloud (VPC)
IP address: a public IP address
Security group: inbound traffic allowed on ports 80, 22, and 8080. For more information, see Add a security group rule.
Deploy Docker
- Connect to an ECS instance. For more information about connection methods, see Connection method overview.
Install Docker.
Alibaba Cloud Linux 3
Run the following command to add the DNF repository of Docker Community Edition (Docker-CE):
sudo dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Run the following command to install the DNF repository plug-in that is dedicated to Alibaba Cloud Linux 3:
sudo dnf -y install dnf-plugin-releasever-adapter --repo alinux3-plus
Run the following command to install Docker:
sudo dnf -y install docker-ce --nobest
If you run the command and an error message similar to the following one is returned, commend out the CentOS repository under the /etc/yum.repos.d directory and then reinstall Docker-CE.
Alibaba Cloud Linux 2
Run the following command to download the YUM repository of Docker-CE:
sudo wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Run the following command to install the YUM repository plug-in that is dedicated to Alibaba Cloud Linux 2:
sudo yum install yum-plugin-releasever-adapter --disablerepo=* --enablerepo=plus
Run the following command to install Docker:
sudo yum -y install docker-ce
CentOS 7.x
Run the following command to download the YUM repository of Docker-CE:
sudo wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Run the following command to install Docker:
sudo yum -y install docker-ce
CentOS 8.x
Change the CentOS 8 repository address.
CentOS 8 has reached its end of life (EOL). In accordance with Linux community rules, all content has been removed from the following CentOS 8 repository address: http://mirror.centos.org/centos/8/. If you continue to use the default CentOS 8 repository configurations on Alibaba Cloud, an error is reported. To use specific installation packages of CentOS 8, change the CentOS 8 repository address. For more information, see Change CentOS 8 repository addresses.
Run the following command to install Dandified YUM (DNF):
sudo yum -y install dnf
Run the following command to install the dependency of the Docker storage driver:
sudo dnf install -y device-mapper-persistent-data lvm2
Run the following command to add a stable Docker repository:
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 repository is added:
sudo dnf list docker-ce
A command output similar to the following one indicates that the Docker repository is added.
Run the following command to install Docker:
sudo dnf install -y docker-ce --nobest
Run the following command to check whether Docker is installed:
sudo docker -v
A command output similar to the following one indicates that Docker is installed.
Run the following command to start Docker and configure Docker to run on system startup:
sudo systemctl start docker sudo systemctl enable docker
Run the following command to check whether Docker is started:
sudo systemctl status docker
A command output similar to the following one indicates that Docker is started.
Basic Docker usage
This section describes only the basic usage of Docker. For information about more Docker usage, visit the Docker official website.
Manage the Docker daemon
sudo systemctl start docker # Run the Docker daemon. sudo systemctl stop docker # Stop the Docker daemon. sudo systemctl restart docker # Restart the Docker daemon. sudo systemctl enable docker # Configure Docker to start on system startup. sudo systemctl status docker # Check the status of Docker
Manage images
In this example, Apache images from Alibaba Cloud Container Registry are used to describe how to use Docker to manage images.
Pull an image.
sudo docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
Modify tags. The names of images from Alibaba Cloud Container Registry are long. Use tags to make the images easy to identify.
sudo docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1
View existing images.
sudo docker images
Forcefully delete an image.
sudo docker rmi -f registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
Manage containers
To query the <Image ID> value for use in the following code snippets, run the
docker images
command.Start a new container.
sudo docker run -it <Image ID> /bin/bash
Start a new container in the background and specify the name of the container.
sudo docker run -d --name <Container name> <Image ID>
Query the container ID.
sudo docker ps
Create an image from the container.
sudo docker commit <Container ID or container name> <Repository name >:<Tag>
Use Docker to create an image
This section describes how to create a simple custom NGINX image from a Dockerfile.
Run the following command to pull an image. In this example, an Apache image from Alibaba Cloud Container Registry is pulled.
sudo docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
Modify the tag of the image to make the image easier to identify.
sudo docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1
Run the following command to create and edit a Dockerfile.
Run the following command to create and edit a Dockerfile:
vim Dockerfile
Press the
I
key to enter Insert mode and add the following content to the file:#Declare a base image. FROM aliweb:v1 #Declare the owner of the base image. MAINTAINER DTSTACK #Specify the commands that you want to run before the container starts. You must append the commands to the end of the RUN command. A Dockerfile can contain up to 127 lines. If the total length of your commands exceeds 127 lines, we recommend that you write the commands to a script. RUN mkdir /dtstact #Specify the commands that are run on system startup. The last command must be a frontend command that runs constantly. Otherwise, the container exits after all the commands are run. ENTRYPOINT ping www.aliyun.com
Press the
Esc
key, enter:wq
, and then press theEnter
key to save and close the Dockerfile.
Run the following command to create an image based on the base NGINX image.
Create the command in the following format:
docker build -t <Image name>:<Image version>.
. The period(.)
at the end of the command indicates the path of the Dockerfile. You must add the period (.). For example, to create an image with the name aliweb and version v2, run the following command:sudo docker build -t aliweb:v2 .
Run the following command to check whether the image is created:
sudo docker images
A command output similar to the one displayed in the following figure indicates that the image is created.
Install and use Docker Compose
Compose is an open-source container orchestration tool provided by the Docker team to define and run multiple containers. With Docker Compose, you use a YAML file to configure the services of your application, run a command to parse the configurations of the YAML file, and then create and start all the services from the configurations. Compose reduces O&M costs and improves deployment efficiency.
For more information about Docker Compose, visit the Docker official website.
Docker Compose is supported only in Python 3 and later. Make sure that 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 check whether Docker Compose is installed:
docker-compose --version
If the version of Docker Compose is returned, Docker Compose is installed.
Use Docker Compose to deploy an application
This section describes how to use Docker Compose to deploy WordPress.
Create and edit the docker-compose.yaml file.
Run the following command to create the docker-compose.yaml file:
vim docker-compose.yaml
Press the
I
key to enter Insert mode and add the following content to the file.In this example, the content that is used to install WordPress is added.
version: '3.1' # Specify the version of Docker Compose. services: wordpress: # Specify a service name. image: wordpress # Specify an image name. restart: always # Configure the container to start each time Docker starts. ports: - 80:80 # Specify port mappings. environment: # Configure environment variables. WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: 123456 WORDPRESS_DB_NAME: wordpress volumes: # Configure mappings between containers and ECS volumes. - wordpress:/var/www/html db: # Specify a service name. image: mysql:5.7 # Specify an image name. restart: always # Configure the container to start each time Docker starts. ports: - 3306:3306 # Specify port mappings. environment: # Configure environment variables. MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: 123456 MYSQL_RANDOM_ROOT_PASSWORD: '1' volumes: # Configure mappings between containers and ECS volumes. - db:/var/lib/mysql volumes: wordpress: db:
Press the
Esc
key to exit Insert mode, enter:wq
, and then press the Enter key to save and close the file.
Run the following command to start the application:
sudo env "PATH=$PATH" docker-compose up -d
Enter an address in the
https://<Public IP address of the ECS instance>
format in the address bar of your browser to go to the WordPress configuration page. You can configure the parameters as prompted to access WordPress.