This topic describes how to install and use Docker and Docker Compose on an Elastic Compute Service (ECS) instance.
Install Docker
Alibaba Cloud Linux 3
Before you install the latest version of Docker, uninstall the current version of Docker and related components to prevent potential installation conflicts and compatibility issues.
Run the following commands to install Docker Community Edition (Docker-CE).
NoteWhen you use the Docker Compose plug-in, do not include a hyphen (-) between docker and compose in a command. Sample command:
docker compose version
.# Add a Docker repository. sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo # Install the Dandified YUM (DNF) repository plug-in that is exclusive to Alibaba Cloud Linux 3. sudo dnf -y install dnf-plugin-releasever-adapter --repo alinux3-plus # Install Docker-CE, the containerd.io container runtime, and the Docker Build and Compose plug-ins. sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin # Start Docker. sudo systemctl start docker # Configure the Docker daemon to start on system startup. sudo systemctl enable docker
Query the Docker version to check whether Docker is installed.
sudo docker -v
If the following command output is returned, Docker is installed on the ECS instance.
Alibaba Cloud Linux 2
Before you install the latest version of Docker, uninstall the current version of Docker and related components to prevent potential installation conflicts and compatibility issues.
Run the following commands to install Docker Community Edition (Docker-CE).
NoteWhen you use the Docker Compose plug-in, do not include a hyphen (-) between docker and compose in a command. Sample command:
docker compose version
.# Add a Docker repository. sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo # Install the Yellowdog Updater Modified (YUM) repository plug-in that is exclusive to Alibaba Cloud Linux 2. sudo yum install yum-plugin-releasever-adapter --disablerepo=* --enablerepo=plus # Install Docker-CE, the containerd.io container runtime, and the Docker Build and Compose plug-ins. sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin # Start Docker. sudo systemctl start docker # Configure the Docker daemon to start on system startup. sudo systemctl enable docker
Query the Docker version to check whether Docker is installed.
sudo docker -v
If the following command output is returned, Docker is installed on the ECS instance.
CentOS 7.x
Before you install the latest version of Docker, uninstall the current version of Docker and related components to prevent potential installation conflicts and compatibility issues.
Run the following commands to install Docker Community Edition (Docker-CE).
NoteWhen you use the Docker Compose plug-in, do not include a hyphen (-) between docker and compose in a command. Sample command:
docker compose version
.# Add a Docker repository. sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo # Install Docker-CE, the containerd.io container runtime, and the Docker Build and Compose plug-ins. sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin # Start Docker. sudo systemctl start docker # Configure the Docker daemon to start on system startup. sudo systemctl enable docker
Query the Docker version to check whether Docker is installed.
sudo docker -v
If the following command output is returned, Docker is installed on the ECS instance.
CentOS 8.x
Before you install the latest version of Docker, uninstall the current version of Docker and related components to prevent potential installation conflicts and compatibility issues.
Run the following commands to install Docker Community Edition (Docker-CE).
NoteWhen you use the Docker Compose plug-in, do not include a hyphen (-) between docker and compose in a command. Sample command:
docker compose version
.# Add a Docker repository. sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo # Install Docker-CE, the containerd.io container runtime, and the Docker Build and Compose plug-ins. sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin # Start Docker. sudo systemctl start docker # Configure the Docker daemon to start on system startup. sudo systemctl enable docker
Query the Docker version to check whether Docker is installed.
sudo docker -v
If the following command output is returned, Docker is installed on the ECS instance.
Ubuntu
Before you install the latest version of Docker, uninstall the current version of Docker and related components to prevent potential installation conflicts and compatibility issues.
Run the following commands to install Docker Community Edition (Docker-CE).
NoteWhen you use the Docker Compose plug-in, do not include a hyphen (-) between docker and compose in a command. Sample command:
docker compose version
.# Update the package manager. sudo apt-get update # Add a Docker repository. sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common sudo curl -fsSL http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository -y "deb [arch=$(dpkg --print-architecture)] http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" # Install Docker-CE, the containerd.io container runtime, and the Docker Build and Compose plug-ins. sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin # Start Docker. sudo systemctl start docker # Configure the Docker daemon to start on system startup. sudo systemctl enable docker
Query the Docker version to check whether Docker is installed.
sudo docker -v
If the following command output is returned, Docker is installed on the ECS instance.
Debian
Before you install the latest version of Docker, uninstall the current version of Docker and related components to prevent potential installation conflicts and compatibility issues.
Run the following commands to install Docker Community Edition (Docker-CE).
NoteWhen you use the Docker Compose plug-in, do not include a hyphen (-) between docker and compose in a command. Sample command:
docker compose version
.# Update the package manager. sudo apt-get update # Add a Docker repository. sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common sudo curl -fsSL http://mirrors.cloud.aliyuncs.com/docker-ce/linux/debian/gpg | sudo apt-key add - sudo add-apt-repository -y "deb [arch=$(dpkg --print-architecture)] http://mirrors.cloud.aliyuncs.com/docker-ce/linux/debian $(lsb_release -cs) stable" # Install Docker-CE, the containerd.io container runtime, and the Docker Build and Compose plug-ins. sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin # Start Docker. sudo systemctl start docker # Configure the Docker daemon to start on system startup. sudo systemctl enable docker
Query the Docker version to check whether Docker is installed.
sudo docker -v
If the following command output is returned, Docker is installed on the ECS instance.
Fedora
Before you install the latest version of Docker, uninstall the current version of Docker and related components to prevent potential installation conflicts and compatibility issues.
Run the following commands to install Docker Community Edition (Docker-CE).
NoteWhen you use the Docker Compose plug-in, do not include a hyphen (-) between docker and compose in a command. Sample command:
docker compose version
.# Add a Docker repository. sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/fedora/docker-ce.repo sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo # Install Docker-CE, the containerd.io container runtime, and the Docker Build and Compose plug-ins. sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin # Start Docker. sudo systemctl start docker # Configure the Docker daemon to start on system startup. sudo systemctl enable docker
Query the Docker version to check whether Docker is installed.
sudo docker -v
If the following command output is returned, Docker is installed on the ECS instance.
Red Hat
Before you install the latest version of Docker, uninstall the current version of Docker and related components to prevent potential installation conflicts and compatibility issues.
Run the following commands to install Docker Community Edition (Docker-CE).
NoteWhen you use the Docker Compose plug-in, do not include a hyphen (-) between docker and compose in a command. Sample command:
docker compose version
.# Add a Docker repository. sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/rhel/docker-ce.repo sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo # Install Docker-CE, the containerd.io container runtime, and the Docker Build and Compose plug-ins. sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin # Start Docker. sudo systemctl start docker # Configure the Docker daemon to start on system startup. sudo systemctl enable docker
Query the Docker version to check whether Docker is installed.
sudo docker -v
If the following command output is returned, Docker is installed on the ECS instance.
Anolis OS
Before you install the latest version of Docker, uninstall the current version of Docker and related components to prevent potential installation conflicts and compatibility issues.
Run the following commands to install Docker Community Edition (Docker-CE).
NoteWhen you use the Docker Compose plug-in, do not include a hyphen (-) between docker and compose in a command. Sample command:
docker compose version
.# Add a Docker repository. sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo # Install Docker-CE, the containerd.io container runtime, and the Docker Build and Compose plug-ins. sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin # Start Docker. sudo systemctl start docker # Configure the Docker daemon to start on system startup. sudo systemctl enable docker
Query the Docker version to check whether Docker is installed.
sudo docker -v
If the following command output is returned, Docker is installed on the ECS instance.
Create a simple web application image
The following section describes how to create a simple web application image based on an NGINX image.
Create a web application image.
# Pull the NGINX image. sudo docker pull nginx:latest # Create a Dockerfile to specify NGINX as the base image and create an index.html file in the root directory of the web server. The index.html file is used to display Hello World!. sudo tee Dockerfile <<-'EOF' FROM nginx:latest RUN echo 'Hello World!' > /usr/share/nginx/html/index.html EOF # Create a web application image named hello-world. sudo docker build . -t hello-world:latest
Start the container created based on the web application image and name the container
hello-world
.sudo docker run -d -p 80:80 --name hello-world hello-world:latest
Run the
curl http://localhost
command to check whether the web application image displaysHello World!
as expected.
Install Docker Compose
If you perform the operations described in this topic to install Docker on an ECS instance, the Docker Compose plug-in is automatically installed on the instance. Run the following command to query the version of Docker Compose:
sudo docker compose version
Expected output: Docker Compose version vN.N.N
. vN.N.N indicates the version of Docker Compose.
If the Docker Compose plug-in is not installed on the ECS instance or you want to use standalone Docker Compose, perform the following steps.
If you use standalone Docker Compose, include a hyphen (-) between docker and compose in a command. Sample command:
docker-compose up
.If you use the Docker Compose plug-in, do not include a hyphen (-) between docker and compose in a command. Sample command:
docker compose up
.
Docker Compose plug-in
Alibaba Cloud Linux 3, CentOS 8.x, and Fedora
sudo dnf -y install docker-compose-plugin
Ubuntu and Debian
sudo apt-get -y install docker-compose-plugin
Alibaba Cloud Linux 2, CentOS 7.x, Red Hat, and Anolis OS
sudo yum -y install docker-compose-plugin
Query the version of Docker Compose to check whether the Docker Compose plug-in is installed.
sudo docker compose version
Standalone Docker Compose
Download and install standalone Docker Compose.
NoteWhen you access GitHub, exceptions may occur, such as access timeout or connection failures, due to network issues. To resolve the exceptions, try to reconnect multiple times.
curl -SL https://github.com/docker/compose/releases/download/v2.30.3/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose # Grant execute permissions on the standalone binary file in the path in which you want to install standalone Docker Compose. sudo chmod +x /usr/local/bin/docker-compose sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
Query the version of Docker Compose to check whether standalone Docker Compose is installed.
sudo docker-compose version
Use Docker Compose to deploy an application
The following section describes how to create a web application based on a WordPress image.
Create a Docker Compose orchestration file and start the WordPress service.
# Create a Docker Compose orchestration file and add MySQL and WordPress to the file. sudo tee docker-compose.yaml <<-'EOF' version: '3.1' services: wordpress: image: wordpress restart: always ports: - "80:80" environment: # The address of the database. WORDPRESS_DB_HOST: db # The username that is used to log on to the database. WORDPRESS_DB_USER: wordpress # The password that is used to log on to the database. WORDPRESS_DB_PASSWORD: 123456 # The name of the database. WORDPRESS_DB_NAME: wordpress volumes: - wordpress:/var/www/html db: image: mysql:5.7 restart: always ports: - "3306:3306" environment: # The name of the database. MYSQL_DATABASE: wordpress # The username that is used to log on to the database. MYSQL_USER: wordpress # The password that is used to log on to the database. MYSQL_PASSWORD: 123456 # The password of the root user of the database. MYSQL_RANDOM_ROOT_PASSWORD: '1' volumes: - db:/var/lib/mysql volumes: wordpress: db: EOF # Start the service container. sudo env "PATH=$PATH" docker compose up -d
Enter an address in the
http://<Public IP address of the ECS instance>
format in the address bar of your browser to access WordPress.NoteYou must open port
80
in a security group to which the ECS instance belongs. For information about how to open a port in a security group, see Add a security group rule.
FAQ
What do I do if an error occurs when I run the dnf config-manager command?
What do I do if an error occurs when I run the dnf install docker-ce command?
References
For information about how to use Docker, see the Docker documentation.
Alibaba Cloud Container Registry releases the artifact center to provide developers with secure and trusted base container images from the Alibaba Cloud website and OpenAnolis community free of charge. For more information, see Overview of the artifact center.
You can use the P2P acceleration feature of Container Registry Enterprise Edition to accelerate image pulling. 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.