All Products
Search
Document Center

Elastic Compute Service:Install and use Docker and Docker Compose

Last Updated:Sep 16, 2025

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.

Uninstall older versions of docker

  1. Uninstall older Docker versions, related software packages and repositories.

    You can safely ignore "package not installed" messages.
    # Remove Docker repositories
    sudo rm -f /etc/yum.repos.d/docker*.repo
    # Uninstall Docker and related software packages
    sudo dnf -y remove \
    docker-ce \
    containerd.io \
    docker-ce-rootless-extras \
    docker-buildx-plugin \
    docker-ce-cli \
    docker-compose-plugin
  2. Uninstalling Docker does not automatically remove existing images, containers, volumes, or networks. By default, Docker stores this data in the /var/lib/docker/ directory. You must manually delete this directory to remove all data.

  1. Install the Docker Community Edition.

    If you are not on an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://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-plugin
  2. Start 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.

Uninstall older versions of docker

  1. Uninstall older Docker versions, related software packages and repositories.

    You can safely ignore "package not installed" messages.
    # Remove Docker repositories
    sudo rm -f /etc/apt/sources.list.d/*docker*.list
    # Uninstall Docker and related software packages
    for pkg in docker.io docker-buildx-plugin docker-ce-cli docker-ce-rootless-extras docker-compose-plugin docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove -y $pkg; done
  2. Uninstalling Docker does not automatically remove existing images, containers, volumes, or networks. By default, Docker stores this data in the /var/lib/docker/ directory. You must manually delete this directory to remove all data.

  1. Install the Docker Community Edition.

    If you are not on an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://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-plugin
    
  2. Start 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.

Uninstall older versions of docker

  1. Uninstall older Docker versions, related software packages and repositories.

    You can safely ignore "package not installed" messages.
    # Remove Docker repositories
    sudo rm -f /etc/apt/sources.list.d/*docker*.list
    # Uninstall Docker and related software packages
    for pkg in docker.io docker-buildx-plugin docker-ce-cli docker-ce-rootless-extras docker-compose-plugin docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove -y $pkg; done
  2. Uninstalling Docker does not automatically remove existing images, containers, volumes, or networks. By default, Docker stores this data in the /var/lib/docker/ directory. You must manually delete this directory to remove all data.

  1. Install the Docker Community Edition.

    If you are not on an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://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-plugin
  2. Start 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.

Uninstall older versions of docker

  1. Uninstall older Docker versions, related software packages and repositories.

    You can safely ignore "package not installed" messages.
    # Remove Docker-related sources
    sudo rm -f /etc/yum.repos.d/docker*.repo
    # Uninstall old versions of Docker and related packages
    sudo yum -y remove \
    docker-ce \
    containerd.io \
    docker-ce-rootless-extras \
    docker-buildx-plugin \
    docker-ce-cli \
    docker-compose-plugin
  2. Uninstalling Docker does not automatically remove existing images, containers, volumes, or networks. By default, Docker stores this data in the /var/lib/docker/ directory. You must manually delete this directory to remove all data.

  1. Install the Docker Community Edition.

    If you are not on an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://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-plugin
  2. Start 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.

Uninstall older versions of docker

  1. Uninstall older Docker versions, related software packages and repositories.

    You can safely ignore "package not installed" messages.
    # Remove Docker repositories
    sudo rm -f /etc/yum.repos.d/docker*.repo
    # Uninstall Docker and related software packages
    sudo dnf -y remove \
    docker-ce \
    containerd.io \
    docker-ce-rootless-extras \
    docker-buildx-plugin \
    docker-ce-cli \
    docker-compose-plugin
  2. Uninstalling Docker does not automatically remove existing images, containers, volumes, or networks. By default, Docker stores this data in the /var/lib/docker/ directory. You must manually delete this directory to remove all data.

  1. Install the Docker Community Edition.

    If you are not on an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://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-plugin
  2. Start 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.

Uninstall older versions of docker

  1. Uninstall older Docker versions, related software packages and repositories.

    You can safely ignore "package not installed" messages.
    # Remove Docker-related sources
    sudo rm -f /etc/yum.repos.d/docker*.repo
    # Uninstall old versions of Docker and related packages
    sudo yum -y remove \
    docker-ce \
    containerd.io \
    docker-ce-rootless-extras \
    docker-buildx-plugin \
    docker-ce-cli \
    docker-compose-plugin
  2. Uninstalling Docker does not automatically remove existing images, containers, volumes, or networks. By default, Docker stores this data in the /var/lib/docker/ directory. You must manually delete this directory to remove all data.

  1. Install the Docker Community Edition.

    If you are not on an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://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-plugin
  2. Start 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.

Uninstall older versions of docker

  1. Uninstall older Docker versions, related software packages and repositories.

    You can safely ignore "package not installed" messages.
    # Remove Docker-related sources
    sudo rm -f /etc/yum.repos.d/docker*.repo
    # Uninstall old versions of Docker and related packages
    sudo yum -y remove \
    docker-ce \
    containerd.io \
    docker-ce-rootless-extras \
    docker-buildx-plugin \
    docker-ce-cli \
    docker-compose-plugin
  2. Uninstalling Docker does not automatically remove existing images, containers, volumes, or networks. By default, Docker stores this data in the /var/lib/docker/ directory. You must manually delete this directory to remove all data.

  1. Install the Docker Community Edition.

    If you are not on an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://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-plugin
  2. Start 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.

Uninstall older versions of docker

  1. Uninstall older Docker versions, related software packages and repositories.

    You can safely ignore "package not installed" messages.
    # Remove Docker-related sources
    sudo rm -f /etc/yum.repos.d/docker*.repo
    # Uninstall old versions of Docker and related packages
    sudo yum -y remove \
    docker-ce \
    containerd.io \
    docker-ce-rootless-extras \
    docker-buildx-plugin \
    docker-ce-cli \
    docker-compose-plugin
  2. Uninstalling Docker does not automatically remove existing images, containers, volumes, or networks. By default, Docker stores this data in the /var/lib/docker/ directory. You must manually delete this directory to remove all data.

  1. Install the Docker Community Edition.

    If you are not on an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://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-plugin
  2. Start 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.

Uninstall older versions of docker

  1. Uninstall older Docker versions, related software packages and repositories.

    You can safely ignore "package not installed" messages.
    # Remove Docker repositories
    sudo rm -f /etc/yum.repos.d/docker*.repo
    # Uninstall Docker and related software packages
    sudo dnf -y remove \
    docker-ce \
    containerd.io \
    docker-ce-rootless-extras \
    docker-buildx-plugin \
    docker-ce-cli \
    docker-compose-plugin
  2. Uninstalling Docker does not automatically remove existing images, containers, volumes, or networks. By default, Docker stores this data in the /var/lib/docker/ directory. You must manually delete this directory to remove all data.

  1. Install the Docker Community Edition.

    If you are not on an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://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-plugin
  2. Start 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

  1. 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:latest
  2. Start a container named hello-world using the web application image.

    sudo docker run -d -p 80:80 --name hello-world hello-world:latest
  3. Run curl http://localhost to verify that the web application correctly returns Hello 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-plugin

Ubuntu, Debian

sudo apt-get -y install docker-compose-plugin

Use Docker Compose to deploy an application

  1. 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:
    EOF
  2. Start 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 -d
  3. In a browser, visit http://<Public_IP_Address_of_your_ECS_instance> to access WordPress.

    Ensure that you allow inbound traffic on port 80 in your security group.

Troubleshooting

Configure Docker permissions for non-root users

By default, Docker commands require root privileges (using sudo). To run docker commands without sudo and adhere to the principle of least privilege, add your user to the docker group.

  1. Add the current user to the docker group.

    sudo usermod -aG docker $USER
  2. Run newgrp docker to apply the change. You can then run docker commands without sudo.

Cannot access http://mirrors.cloud.aliyuncs.com

  • Symptom: The address http://mirrors.cloud.aliyuncs.com is unreachable.

  • Cause: The network may be unstable.

  • Solution: Replace http://mirrors.cloud.aliyuncs.com with https://mirrors.aliyun.com.

    To use https://mirrors.aliyun.com, your instance must have Public Bandwidth enabled. For instructions, see Enable an Internet connection.

dnf config-manager command fails

  • Symptom: When running the dnf config-manager command on a Linux instance, the following error occurs:

    /usr/lib/python3/dist-packages/dnf/const.py:22: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives.
  • Cause: The dnf package is outdated.

  • Solution: Try updating the dnf package to the latest version with sudo dnf update dnf, then try to install docker again.

dnf install docker-ce command fails

  • Symptom: When running the dnf -y install docker-ce command on a Linux instance, the following error occurs:

    (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.
  • Cause: The software package cache is outdated.

  • Solution: Run sudo dnf clean packages to clear the package cache, then try to install docker again.

  • Symptom: When running the dnf -y install docker-ce command on a Linux instance, the following error occurs:

    CentOS- Base                                                                                                                         0.0  B/s |   0  B     00:30    
    Errors during downloading metadata for repository 'base':
      - Curl error (6): Couldn't resolve host name for http://mirror.centos.org/os/BaseOS/x86_64/os/repodata/repomd.xml [Could not resolve host: mirror.aliyuncs.com]
      - Curl error (28): Timeout was reached for http://mirror.centos.org/os/BaseOS/x86_64/os/repodata/repomd.xml [Connection timed out after 30000 milliseconds]
      - Curl error (6): Couldn't resolve host name for http://mirror.centos.org/os/BaseOS/x86_64/os/repodata/repomd.xml [Could not resolve host: mirror.cloud.aliyuncs.com]
    Error: Failed to download metadata for repo 'base': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried
  • Cause: The default CentOS 8 repository are expired.

  • Solution: Follow the steps below to replace the expired CentOS 8 repository with a valid one provided by Alibaba Cloud.

    # Back up the expired repository file.
    sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    # Download the Alibaba Cloud-provided CentOS-Base.repo to /etc/yum.repos.d/.
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.aliyuncs.com/repo/Centos-vault-8.5.2111.repo
    # Configure the repository to use internal mirror addresses for ECS instances.
    sudo sed -i 's/mirrors.cloud.aliyuncs.com/url_tmp/g' /etc/yum.repos.d/CentOS-Base.repo && sudo sed -i 's/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g' /etc/yum.repos.d/CentOS-Base.repo && sudo sed -i 's/url_tmp/mirrors.aliyun.com/g' /etc/yum.repos.d/CentOS-Base.repo
    # Clear the old cache and generate a new one.
    sudo yum clean all && sudo yum makecache

    Try to install docker again after completing these steps.

References