This topic explains how to install Docker and Docker Compose on various Linux distributions. You can package applications and their dependencies into portable containers to standardize deployments and simplify managing multi-service applications.
Install Docker
Alibaba Cloud Linux 3
To prevent conflicts and compatibility issues, uninstall any existing Docker components first.
Install the Docker Community Edition.
If you are not on an Alibaba Cloud server, replace
http://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.# Add the Docker package 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 dnf repository compatibility plugin for Alibaba Cloud Linux 3 sudo dnf -y install dnf-plugin-releasever-adapter --repo alinux3-plus # Install Docker Community Edition, the containerd.io container runtime, and the Docker Buildx and Compose plugins sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStart Docker and enable it to start on boot.
# Start Docker sudo systemctl start docker # Enable Docker to start on boot sudo systemctl enable docker
Ubuntu
To prevent conflicts and compatibility issues, uninstall any existing Docker components first.
Install the Docker Community Edition.
If you are not on an Alibaba Cloud server, replace
http://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.# Update the apt package index sudo apt-get update # Add the Docker software package 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 Community Edition, the containerd.io container runtime, and the Docker Buildx and Compose plugins sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStart Docker and enable it to start on boot.
# Start Docker sudo systemctl start docker # Enable Docker to start on boot sudo systemctl enable docker
Debian
To prevent conflicts and compatibility issues, uninstall any existing Docker components first.
Install the Docker Community Edition.
If you are not on an Alibaba Cloud server, replace
http://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.# Update the apt package index sudo apt-get update # Add the Docker software package 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 Community Edition, the containerd.io container runtime, and the Docker Buildx and Compose plugins sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStart Docker and enable it to start on boot.
# Start Docker sudo systemctl start docker # Enable Docker to start on boot sudo systemctl enable docker
Red Hat
To prevent conflicts and compatibility issues, uninstall any existing Docker components first.
Install the Docker Community Edition.
If you are not on an Alibaba Cloud server, replace
http://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.# Add the Docker software package 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 Community Edition, the containerd.io container runtime, and the Docker Buildx and Compose plugins sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStart Docker and enable it to start on boot.
# Start Docker sudo systemctl start docker # Enable Docker to start on boot sudo systemctl enable docker
Fedora
To prevent conflicts and compatibility issues, uninstall any existing Docker components first.
Install the Docker Community Edition.
If you are not on an Alibaba Cloud server, replace
http://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.# Add the Docker software package 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 Community Edition, the containerd.io container runtime, and the Docker Buildx and Compose plugins sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStart Docker and enable it to start on boot.
# Start Docker sudo systemctl start docker # Enable Docker to start on boot sudo systemctl enable docker
Anolis OS
To prevent conflicts and compatibility issues, uninstall any existing Docker components first.
Install the Docker Community Edition.
If you are not on an Alibaba Cloud server, replace
http://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.# Add the Docker software package 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 Community Edition, the containerd.io container runtime, and the Docker Buildx and Compose plugins sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStart Docker and enable it to start on boot.
# Start Docker sudo systemctl start docker # Enable Docker to start on boot sudo systemctl enable docker
Alibaba Cloud Linux 2
To prevent conflicts and compatibility issues, uninstall any existing Docker components first.
Install the Docker Community Edition.
If you are not on an Alibaba Cloud server, replace
http://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.# Add the Docker software package 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 yum repository compatibility plugin for Alibaba Cloud Linux 2 sudo yum install yum-plugin-releasever-adapter --disablerepo=* --enablerepo=plus # Install Docker Community Edition, the containerd.io container runtime, and the Docker Buildx and Compose plugins sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStart Docker and enable it to start on boot.
# Start Docker sudo systemctl start docker # Enable Docker to start on boot sudo systemctl enable docker
CentOS 7.x
To prevent conflicts and compatibility issues, uninstall any existing Docker components first.
Install the Docker Community Edition.
If you are not on an Alibaba Cloud server, replace
http://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.# Add the Docker software package 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 Community Edition, the containerd.io container runtime, and the Docker Buildx and Compose plugins sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStart Docker and enable it to start on boot.
# Start Docker sudo systemctl start docker # Enable Docker to start on boot sudo systemctl enable docker
CentOS 8.x
To prevent conflicts and compatibility issues, uninstall any existing Docker components first.
Install the Docker Community Edition.
If you are not on an Alibaba Cloud server, replace
http://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.# Add the Docker software package 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 Community Edition, the containerd.io container runtime, and the Docker Buildx and Compose plugins sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStart Docker and enable it to start on boot.
# Start Docker sudo systemctl start docker # Enable Docker to start on boot sudo systemctl enable docker
Use Docker to quickly deploy a web application
Build a web application image.
# Pull the latest Nginx image sudo docker pull nginx:latest # Create a Dockerfile based on Nginx to serve a "Hello World!" page. sudo tee Dockerfile <<-'EOF' FROM nginx:latest RUN echo 'Hello World!' > /usr/share/nginx/html/index.html EOF # Build the image and tag it as hello-world:latest sudo docker build . -t hello-world:latestStart a container named
hello-worldusing the web application image.sudo docker run -d -p 80:80 --name hello-world hello-world:latestRun
curl http://localhostto verify that the web application correctly returnsHello World!.
Install Docker Compose
Use Docker Compose to define and manage multi-service applications from a single YAML file, especially when services have startup dependencies.
Alibaba Cloud Linux, CentOS, Red Hat, Anolis, Fedora
sudo yum -y install docker-compose-pluginUbuntu, Debian
sudo apt-get -y install docker-compose-pluginUse Docker Compose to deploy an application
Create an orchestration file named
docker-compose.yaml.# Create the orchestration file and add MySQL and WordPress services sudo tee docker-compose.yaml <<-'EOF' version: '3.1' services: wordpress: image: wordpress restart: always ports: - "80:80" environment: # Database host address WORDPRESS_DB_HOST: wordpress_db # Database username WORDPRESS_DB_USER: wordpress # Database password WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD} # Database name WORDPRESS_DB_NAME: wordpress volumes: - wordpress:/var/www/html db: image: mysql:5.7 restart: always container_name: wordpress_db environment: # Database name MYSQL_DATABASE: wordpress # Database username MYSQL_USER: wordpress # Database password MYSQL_PASSWORD: ${WORDPRESS_DB_PASSWORD} # Database root user password MYSQL_RANDOM_ROOT_PASSWORD: ${WORDPRESS_DB_PASSWORD} volumes: - db:/var/lib/mysql volumes: wordpress: db: EOFStart the WordPress service. Replace
<database_password>in the command with your MySQL database password.Note that the Docker Compose command uses a space (docker compose) instead of a hyphen (docker-compose). For example,
docker compose version.# Start the service containers. sudo env "PATH=$PATH" "WORDPRESS_DB_PASSWORD=<database_password>" docker compose -f docker-compose.yaml up -dIn a browser, visit
http://<Public_IP_Address_of_your_ECS_instance>to access WordPress.Ensure that you allow inbound traffic on port
80in your security group.
Troubleshooting
Configure Docker permissions for non-root users
Cannot access http://mirrors.cloud.aliyuncs.com
dnf config-manager command fails
dnf install docker-ce command fails
References
For more information on using Docker, see official Docker documentation.
Alibaba Cloud's Artifact Center provides free, secure, and trusted base container images from Alibaba Cloud and the OpenAnolis Community.
Accelerate image pulls with the enterprise P2P acceleration feature. For details, see Use P2P acceleration in other container environments.
Manage your Alibaba Cloud resources by running the Alibaba Cloud CLI in a Docker container. For details, see Run the Cloud Assistant CLI in a Docker container.