Docker is an open platform on which you can develop, publish, and run applications. This topic describes how to install and use Docker on an Elastic Compute Service (ECS) instance.
Install Docker
Alibaba Cloud Linux 3
Before you install the latest version of Docker, run the docker -v
command to query the current Docker version. If the command output indicates that the command is unavailable, Docker is not installed. If you installed an earlier version of Docker, you must uninstall Docker and related components.
Update your package management tool.
sudo dnf -y update
Add an Alibaba Cloud repository to your instance. This repository contains the Docker software package that allows you to run commands to install and update Docker.
If your instance can access the Internet, run the following command:
sudo dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
If your instance cannot access the Internet, use the virtual private cloud (VPC) endpoint and run the following command:
sudo dnf config-manager --add-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 the Docker Community Edition (Docker-CE), the container runtime named containerd.io, and Docker building and orchestration plug-ins.
sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Start Docker and enable the Docker daemon to start on system startup. This ensures that the Docker service automatically starts every time the system starts.
NoteIf an error occurs when you run the systemctl command, troubleshoot the error. For more information, see What do I do if an error occurs when I run the systemctl command on a Linux instance?
sudo systemctl start docker 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, run the docker -v
command to query the current Docker version. If the command output indicates that the command is unavailable, Docker is not installed. If you installed an earlier version of Docker, you must uninstall Docker and related components.
Update your package management tool.
sudo yum -y update
Add an Alibaba Cloud repository to your instance. This repository contains the Docker software package that allows you to run commands to install and update Docker.
If your instance can access the Internet, run the following command:
sudo wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
If your instance cannot access the Internet, use the virtual private cloud (VPC) endpoint and run the following command:
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 the Docker Community Edition (Docker-CE), the container runtime named containerd.io, and Docker building and orchestration plug-ins.
sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Start Docker and enable the Docker daemon to start on system startup. This ensures that the Docker service automatically starts every time the system starts.
NoteIf an error occurs when you run the systemctl command, troubleshoot the error. For more information, see What do I do if an error occurs when I run the systemctl command on a Linux instance?
sudo systemctl start docker 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, run the docker -v
command to query the current Docker version. If the command output indicates that the command is unavailable, Docker is not installed. If you installed an earlier version of Docker, you must uninstall Docker and related components.
Update your package management tool.
sudo yum -y update
Add an Alibaba Cloud repository to your instance. This repository contains the Docker software package that allows you to run commands to install and update Docker.
If your instance can access the Internet, run the following command:
sudo wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
If your instance cannot access the Internet, use the virtual private cloud (VPC) endpoint and run the following command:
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 Docker Community Edition (Docker-CE), the container runtime named containerd.io, and Docker building and orchestration plug-ins.
sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Start Docker and enable the Docker daemon to start on system startup. This ensures that the Docker service automatically starts every time the system starts.
NoteIf an error occurs when you run the systemctl command, troubleshoot the error. For more information, see What do I do if an error occurs when I run the systemctl command on a Linux instance?
sudo systemctl start docker 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, run the docker -v
command to query the current Docker version. If the command output indicates that the command is unavailable, Docker is not installed. If you installed an earlier version of Docker, you must uninstall Docker and related components.
Update your package management tool.
sudo dnf -y update
Add an Alibaba Cloud repository to your instance. This repository contains the Docker software package that allows you to run commands to install and update Docker.
If your instance can access the Internet, run the following command:
sudo dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
If your instance cannot access the Internet, use the virtual private cloud (VPC) endpoint and run the following command:
sudo dnf config-manager --add-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 Docker Community Edition (Docker-CE), the container runtime named containerd.io, and Docker building and orchestration plug-ins.
sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Start Docker and enable the Docker daemon to start on system startup. This ensures that the Docker service automatically starts every time the system starts.
NoteIf an error occurs when you run the systemctl command, troubleshoot the error. For more information, see What do I do if an error occurs when I run the systemctl command on a Linux instance?
sudo systemctl start docker 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, run the docker -v
command to query the current Docker version. If the command output indicates that the command is unavailable, Docker is not installed. If you installed an earlier version of Docker, you must uninstall Docker and related components.
Update your package management tool.
sudo apt-get update
Add an Alibaba Cloud repository to your instance. This repository contains the Docker software package that allows you to run commands to install and update Docker.
If your instance can access the Internet, run the following command:
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common sudo curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add - sudo add-apt-repository -y "deb [arch=$(dpkg --print-architecture)] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
If your instance cannot access the Internet, use the virtual private cloud (VPC) endpoint and run the following command:
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 | 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 the Docker Community Edition (Docker-CE), the container runtime named containerd.io, and Docker building and orchestration plug-ins.
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Start Docker and enable the Docker daemon to start on system startup. This ensures that the Docker service automatically starts every time the system starts.
NoteIf an error occurs when you run the systemctl command, troubleshoot the error. For more information, see What do I do if an error occurs when I run the systemctl command on a Linux instance?
sudo systemctl start docker 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, run the docker -v
command to query the current Docker version. If the command output indicates that the command is unavailable, Docker is not installed. If you installed an earlier version of Docker, you must uninstall Docker and related components.
Update your package management tool.
sudo apt-get update
Add an Alibaba Cloud repository to your instance. This repository contains the Docker software package that allows you to run commands to install and update Docker.
If your instance can access the Internet, run the following command:
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common sudo curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | apt-key add - sudo add-apt-repository -y "deb [arch=$(dpkg --print-architecture)] https://mirrors.aliyun.com/docker-ce/linux/debian $(lsb_release -cs) stable" sudo apt-get update
If your instance cannot access the Internet, use the virtual private cloud (VPC) endpoint and run the following command:
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 | 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" sudo apt-get update
Install the Docker Community Edition (Docker-CE), the container runtime named containerd.io, and Docker building and orchestration plug-ins.
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Start Docker and enable the Docker daemon to start on system startup. This ensures that the Docker service automatically starts every time the system starts.
NoteIf an error occurs when you run the systemctl command, troubleshoot the error. For more information, see What do I do if an error occurs when I run the systemctl command on a Linux instance?
sudo systemctl start docker 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, run the docker -v
command to query the current Docker version. If the command output indicates that the command is unavailable, Docker is not installed. If you installed an earlier version of Docker, you must uninstall Docker and related components.
Update your package management tool.
sudo dnf -y update
Add an Alibaba Cloud repository to your instance. This repository contains the Docker software package that allows you to run commands to install and update Docker.
If your instance can access the Internet, run the following command:
sudo dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/fedora/docker-ce.repo
If your instance cannot access the Internet, use the virtual private cloud (VPC) endpoint and run the following command:
sudo dnf config-manager --add-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 the Docker Community Edition (Docker-CE), the container runtime named containerd.io, and Docker building and orchestration plug-ins.
sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Start Docker and enable the Docker daemon to start on system startup. This ensures that the Docker service automatically starts every time the system starts.
NoteIf an error occurs when you run the systemctl command, troubleshoot the error. For more information, see What do I do if an error occurs when I run the systemctl command on a Linux instance?
sudo systemctl start docker 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, run the docker -v
command to query the current Docker version. If the command output indicates that the command is unavailable, Docker is not installed. If you installed an earlier version of Docker, you must uninstall Docker and related components.
Update your package management tool.
sudo dnf -y update
Add an Alibaba Cloud repository to your instance. This repository contains the Docker software package that allows you to run commands to install and update Docker.
If your instance can access the Internet, run the following command:
sudo wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/rhel/docker-ce.repo
If your instance cannot access the Internet, use the virtual private cloud (VPC) endpoint and run the following command:
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 the Docker Community Edition (Docker-CE), the container runtime named containerd.io, and Docker building and orchestration plug-ins.
sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Start Docker and enable the Docker daemon to start on system startup. This ensures that the Docker service automatically starts every time the system starts.
NoteIf an error occurs when you run the systemctl command, troubleshoot the error. For more information, see What do I do if an error occurs when I run the systemctl command on a Linux instance?
sudo systemctl start docker 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, run the docker -v
command to query the current Docker version. If the command output indicates that the command is unavailable, Docker is not installed. If you installed an earlier version of Docker, you must uninstall Docker and related components.
Update your package management tool.
sudo yum -y update
Add an Alibaba Cloud repository to your instance. This repository contains the Docker software package that allows you to run commands to install and update Docker.
If your instance can access the Internet, run the following command:
sudo wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
If your instance cannot access the Internet, use the virtual private cloud (VPC) endpoint and run the following command:
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 Docker Community Edition (Docker-CE), the container runtime named containerd.io, and Docker building and orchestration plug-ins.
sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Start Docker and enable the Docker daemon to start on system startup. This ensures that the Docker service automatically starts every time the system starts.
NoteIf an error occurs when you run the systemctl command, troubleshoot the error. For more information, see What do I do if an error occurs when I run the systemctl command on a Linux instance?
sudo systemctl start docker 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 example describes how to create a simple web application image by using an NGINX image as the base image.
Preparations
On your instance that can access the Internet, run the following docker pull
command to obtain the most recent NGINX image:
sudo docker pull nginx:latest
Create an image
Create the
Dockerfile
file. TheDockerfile
file is a special text file that defines how to build a Docker image. In the Dockerfile file, specify a base image for Docker, and specify the software packages to install and settings to configure on the base image.sudo touch Dockerfile
Modify the created
Dockerfile
file by adding content.FROM nginx:latest RUN echo 'Hello World!' > /usr/share/nginx/html/index.html
Use the Dockerfile file to build your image.
sudo docker build . -t hello-world:latest
Create a container based on your new image and name the container
hello-world
.sudo docker run -d -p 80:80 --name hello-world hello-world:latest
Check whether the image is created. Run the
curl http://localhost
command. Ifhello world!
is returned, the image is created as expected.
FAQs
What do I do if an error occurs when I run the dnf config-manager command on a Linux ECS instance?
What do I do if an error occurs when I run the dnf install docker-ce command on a Linux ECS instance?
References
For information about how to use Docker, see the Docker documentation.
After you follow the procedure described in this topic, Docker Compose is installed on your instance. If Docker Compose is not installed, install Docker Compose as described in the official Docker installation 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.