You can use repositories provided by Alibaba Cloud to quickly install Docker on Elastic Compute Service (ECS) instances. This topic describes how to install Docker by using Alibaba Cloud Linux and CentOS repositories, how to use Docker to create images, and how to deploy Docker Compose, streamlining the process of building, deploying, and managing your applications.
Prepare resources
An ECS instance is created and has the following settings. If no instance is created, create an 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: The instance is assigned a public IP address or associated with an elastic IP address (EIP). For information about how to associate an EIP with an instance, see Associate or disassociate an EIP.
Security group: Inbound rules are added to a security group of the instance to allow traffic on ports 80, 22, and 8080. For more information, see Add a security group rule.
Install Docker
Connect to an ECS instance.
For information about the connection methods, see Connection method overview.
Install Docker.
Alibaba Cloud Linux 3
Run the following command to add the Dandified YUM (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 an error message similar to the following message is returned, run the
sudo dnf clean packages
command to clear the existing package in the cache and re-install Docker-CE.(8-9/12): docker-ce-24.0.7-1.el8.x86_64.rpm 38% [================- ] 8.2 MB/s | 38 MB 00:07 ETA The downloaded packages were saved in cache until the next successful transaction. You can remove cached packages by executing 'dnf clean packages'. Error: Error downloading packages: containerd.io-1.6.26-3.1.el8.x86_64: Cannot download, all mirrors were already tried without success
If an error message similar to the message shown in the following figure is displayed, comment out the CentOS repository in the /etc/yum.repos.d directory and re-install Docker-CE.
Alibaba Cloud Linux 2
Run the following command to download the YUM repository:
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:
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 reached EOL. In accordance with Linux community rules, all content was removed from the following CentOS 8 repository address: http://mirror.centos.org/centos/8/. If you continue to use the default CentOS 8 repository 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 DNF:
sudo yum -y install dnf
Run the following command to install the dependency package 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 output 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 output 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 output indicates that Docker is started.
Basic Docker operations
This section describes only basic Docker usage. For information about more Docker usage, visit 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 the Docker deamon to start on system startup. sudo systemctl status docker # Check the Docker deamon status.
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 easily identify the images.
sudo docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1
View existing images.
sudo docker images
Force 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.Create a container from a specified image.
sudo docker run -it <Image ID> /bin/bash
Create and run a container that has a specified name in the background.
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 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:#Specify a base image. FROM aliweb:v1 #Specify the base image. MAINTAINER DTSTACK #Specify the command that you want to run before the container starts. Append the command to the end of the RUN command. To run many commands, we recommend that you write the commands to a script. Each Docker image can contain up to 127 layers. RUN mkdir /dtstact #Specify the command that are run on system startup. The last command must be a frontend command that runs constantly. This prevents the container from exiting immediately after starting. 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 build an image based on the base NGINX image.
Run the command in the following format:
docker build -t <Image name>:<Image version>.
. The period(.)
at the end of the command indicates the directory of the Dockerfile. The period (.) is required. For example, to build 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 built:
sudo docker images
A command output similar to the one shown in the following figure indicates that the image is built.
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 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:
sudo pip3 install -U pip setuptools
Run the following command to install Docker Compose:
sudo 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 a docker-compose.yaml file:
sudo 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 a port mapping. 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 a port mapping. 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.
References
For more information about how to use Docker, see Docker documentation.
Use Docker images.
Alibaba Cloud Container Registry releases Artifact Center to provide container developers with secure and trusted base container images for free from the Alibaba Cloud website and OpenAnolis community. After you install Docker, you can use the Docker container images provided by Artifact Center to satisfy specific business requirements, such as application deployment, environment development, operating system setup, and AI or big data learning framework construction.
For more information about how to use container images from Artifact Center, see Use an Alibaba Cloud Linux Docker image.
Accelerate Docker image pulling.
You can use the P2P acceleration feature to speed up image pulling and application deployment. For more information, see Use P2P acceleration in other container environments.
You can configure the CLI tool in Docker to manage your Alibaba Cloud resources. For more information, see Configure Alibaba Cloud CLI in Docker.
You can also use Alibaba Cloud Container Registry to run and manage containerized applications.