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.
Prerequisites
An ECS instance that meets the following requirements is created. If no instance is created, create an instance. For more information, see Create an instance on the Custom Launch tab.
Operating system: Alibaba Cloud Linux 3 64-bit, Alibaba Cloud Linux 2 64-bit, CentOS 7.x 64-bit, CentOS 8.x 64-bit, Ubuntu 22, or Ubuntu 20.
Network type: Virtual Private Cloud (VPC).
IP address: The instance is assigned a static public IP address (also known as auto-assigned or system-assigned 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: S
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
Ubuntu 18, Ubuntu 22, Ubuntu 20, Debian 11, or Debian 12
Update the software package list.
sudo apt update
Install the dependencies that are required by Docker.
sudo apt-get -y install ca-certificates curl
Create the
/etc/apt/keyrings
directory and download the official GNU Privacy Guard (GPG) key of Docker to the directory.Ubuntu 18, Ubuntu 22, or Ubuntu 20
sudo install -m 0755 -d /etc/apt/keyrings sudo curl -fsSL http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc
Debian 11 or Debian 12
sudo install -m 0755 -d /etc/apt/keyrings sudo curl -fsSL http://mirrors.cloud.aliyuncs.com/docker-ce/linux/debian/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc
Add the Docker repository to the software repository list of the system.
Ubuntu 18, Ubuntu 22, or Ubuntu 20
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Debian 11 or Debian 12
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] http://mirrors.cloud.aliyuncs.com/docker-ce/linux/debian \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Update the software package list.
sudo apt update
Install Docker.
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
Run the following command to check whether Docker is installed:
docker -v
A command output similar to the following output indicates that Docker is installed.
Run the following commands 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 the official Docker 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 daemon to start on system startup. sudo systemctl status docker # Check the Docker daemon 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
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.Create a container from a specific image.
sudo docker run -it <Image ID> /bin/bash
Create and run a container that has a specific 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 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 easily identify the image.
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 owner of 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 commands that are run on system startup. The last command must be a frontend command that constantly runs. This prevents the container from immediately exiting 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 create 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, run the following command to build an image named aliweb in version v2: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 output shown in the following figure indicates that the image is built.
Install and use Docker Compose
Docker Compose is an open source container orchestration tool developed by the Docker team to define and run multiple containers. In 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. Docker Compose reduces O&M costs and improves deployment efficiency.
For more information about Docker Compose, visit the official Docker 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 from the Alibaba Cloud website and OpenAnolis community free of charge. After you install Docker, you can use the Docker container images in Artifact Center to meet 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 image in Docker.
Accelerate Docker image pulling.
You can use the P2P acceleration feature to accelerate image pulling and application deployment. For more information, see Use the P2P acceleration feature on hosts where Docker is installed.
You can configure the CLI tool in Docker to manage your Alibaba Cloud resources. For more information, see Run Alibaba Cloud CLI in a Docker container.
You can also use Alibaba Cloud Container Registry to run and manage containerized applications.