Install and use Docker and Docker Compose

更新时间:
复制 MD 格式

Install Docker and Docker Compose on various Linux distributions. Package applications and their dependencies into portable containers to standardize deployments and simplify the management of multiple services.

Install Docker

Quick installation

You can use Terraform for a one-click run. This method supports installing Docker on an existing instance or creating a new instance with Docker pre-installed.

  • New instance: Set the create_instance = true parameter, and then click Start Debugging > Preview and Run.

  • Existing instance: Set the create_instance = false and instance_id parameters, and then click Start Debugging > Preview and Run.

Command-line installation

Alibaba Cloud Linux 4

Before you install a new version of Docker, uninstall all existing Docker components. This prevents potential conflicts and compatibility issues.

Uninstall old versions of Docker

  1. Uninstall old versions of Docker, related packages, and software sources.

    # Delete Docker-related sources
    sudo rm -f /etc/yum.repos.d/docker*.repo
    # Uninstall Docker and related packages
    sudo dnf -y remove \  
    docker \
    moby \
    docker-ce \
    containerd.io \
    docker-ce-rootless-extras \
    docker-buildx-plugin \
    docker-ce-cli \
    docker-compose-plugin
  2. Uninstalling Docker does not automatically remove images, containers, persistent volumes (PVs), or networks. This data is stored in the /var/lib/docker/ directory by default. You must delete this directory manually.

  1. Install Docker.

    • Recommended method:

      # Install the Moby runtime
      sudo yum install -y moby
    • Install Docker Community Edition.

      For servers that are not hosted on Alibaba Cloud, replace http://mirrors.cloud.aliyuncs.com with https://mirrors.aliyun.com. You must also select a CentOS-compatible version from the Docker CE repository and uninstall any older Docker versions before installation.
      # Add the Docker-ce package source
      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
      
      # You need to choose which version of CentOS to install, such as centos9 or centos10, and then decide which version to replace releasever with
      # To switch to CentOS 9, run:
      sudo sed -i 's|$releasever|9|' /etc/yum.repos.d/docker-ce.repo
      # To switch to CentOS 10, run:
      sudo sed -i 's|$releasever|10|' /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
      
      # Note: If you want to switch to the native Moby component provided by Alinux4 after use, first remove the docker-ce component and disable the CentOS repo
      sudo yum remove -y docker-ce docker-ce-cli
      sudo rm -rf /etc/yum.repos.d/docker-ce.repo
  2. Start Docker and enable it to start on boot.

    # Start the service and enable it to start on boot
    sudo systemctl start docker
    sudo systemctl enable docker
    
    # Verify the installation
    docker --version  # This will display the installed version
Note
  • The current system has Moby enabled as the default container runtime. Moby will continue to be updated in future versions.

  • For forward compatibility, older Docker components can still be used, but they will no longer be updated.

Alibaba Cloud Linux 3

Before you install a new version of Docker, uninstall all existing Docker components. This prevents potential conflicts and compatibility issues.

Uninstall old versions of Docker

  1. Uninstall old versions of Docker, related packages, and software sources.

    You can ignore any messages that a package is not installed. This indicates that the package was not present.
    # Delete Docker-related sources
    sudo rm -f /etc/yum.repos.d/docker*.repo
    # Uninstall Docker and related 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 images, containers, persistent volumes (PVs), or networks. This data is stored in the /var/lib/docker/ directory by default. You must delete this directory manually.

  1. Install Docker Community Edition.

    If you are not using an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://mirrors.aliyun.com
    # Add the Docker package source
    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
    # DNF source 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
    # Set the Docker daemon to start automatically on system boot
    sudo systemctl enable docker

Ubuntu

Before you install a new version of Docker, uninstall all existing Docker components. This prevents potential conflicts and compatibility issues.

Uninstall old versions of Docker

  1. Uninstall old versions of Docker, related packages, and software sources.

    You can ignore any messages that a package is not installed. This indicates that the package was not present.
    # Delete Docker-related sources
    sudo rm -f /etc/apt/sources.list.d/*docker*.list
    # Uninstall Docker and related 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 images, containers, persistent volumes (PVs), or networks. This data is stored in the /var/lib/docker/ directory by default. You must delete this directory manually.

  1. Install Docker Community Edition.

    If you are not using an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://mirrors.aliyun.com
    # Update the package management tool
    sudo apt-get update
    # Add the Docker package source
    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
    # Set the Docker daemon to start automatically on system boot
    sudo systemctl enable docker

Debian

Before you install a new version of Docker, uninstall all existing Docker components. This prevents potential conflicts and compatibility issues.

Uninstall old versions of Docker

  1. Uninstall old versions of Docker, related packages, and software sources.

    You can ignore any messages that a package is not installed. This indicates that the package was not present.
    # Delete Docker-related sources
    sudo rm -f /etc/apt/sources.list.d/*docker*.list
    # Uninstall Docker and related 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 images, containers, persistent volumes (PVs), or networks. This data is stored in the /var/lib/docker/ directory by default. You must delete this directory manually.

  1. Install Docker Community Edition.

    If you are not using an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://mirrors.aliyun.com
    Important

    For Debian 10 and earlier, you must first replace the source.

    # Add the GPG key
    sudo apt update
    sudo apt install ca-certificates curl
    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 software source to the Apt source list.
    sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
    Types: deb
    URIs: http://mirrors.cloud.aliyuncs.com/docker-ce/linux/debian
    Suites: $(. /etc/os-release && echo "$VERSION_CODENAME")
    Components: stable
    Signed-By: /etc/apt/keyrings/docker.asc
    EOF
    
    sudo apt update
    # Install Docker Community Edition, the containerd.io container runtime, and the Docker Buildx and Compose plugins
    sudo apt install -y 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
    # Set the Docker daemon to start automatically on system boot
    sudo systemctl enable docker

Red Hat

Before you install a new version of Docker, uninstall all existing Docker components. This prevents potential conflicts and compatibility issues.

Uninstall old versions of Docker

  1. Uninstall old versions of Docker, related packages, and software sources.

    You can ignore any messages that a package is not installed. This indicates that the package was not present.
    # Delete 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 images, containers, persistent volumes (PVs), or networks. This data is stored in the /var/lib/docker/ directory by default. You must delete this directory manually.

  1. Install Docker Community Edition.

    If you are not using an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://mirrors.aliyun.com
    # Add the Docker package source
    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
    # Set the Docker daemon to start automatically on system boot
    sudo systemctl enable docker

Fedora

Before you install a new version of Docker, uninstall all existing Docker components. This prevents potential conflicts and compatibility issues.

Uninstall old versions of Docker

  1. Uninstall old versions of Docker, related packages, and software sources.

    You can ignore any messages that a package is not installed. This indicates that the package was not present.
    # Delete Docker-related sources
    sudo rm -f /etc/yum.repos.d/docker*.repo
    # Uninstall Docker and related 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 images, containers, persistent volumes (PVs), or networks. This data is stored in the /var/lib/docker/ directory by default. You must delete this directory manually.

  1. Install Docker Community Edition.

    If you are not using an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://mirrors.aliyun.com
    # Add the Docker package source
    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
    # Set the Docker daemon to start automatically on system boot
    sudo systemctl enable docker

Anolis OS

Before you install a new version of Docker, uninstall all existing Docker components. This prevents potential conflicts and compatibility issues.

Uninstall old versions of Docker

  1. Uninstall old versions of Docker, related packages, and software sources.

    You can ignore any messages that a package is not installed. This indicates that the package was not present.
    # Delete 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 images, containers, persistent volumes (PVs), or networks. This data is stored in the /var/lib/docker/ directory by default. You must delete this directory manually.

  1. Install Docker Community Edition.

    If you are not using an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://mirrors.aliyun.com
    # Add the Docker package source
    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
    # Set the Docker daemon to start automatically on system boot
    sudo systemctl enable docker

Alibaba Cloud Linux 2

Before you install a new version of Docker, uninstall all existing Docker components. This prevents potential conflicts and compatibility issues.

Uninstall old versions of Docker

  1. Uninstall old versions of Docker, related packages, and software sources.

    You can ignore any messages that a package is not installed. This indicates that the package was not present.
    # Delete 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 images, containers, persistent volumes (PVs), or networks. This data is stored in the /var/lib/docker/ directory by default. You must delete this directory manually.

  1. Install Docker Community Edition.

    If you are not using an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://mirrors.aliyun.com
    # Add the Docker package source
    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
    # YUM source 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
    # Set the Docker daemon to start automatically on system boot
    sudo systemctl enable docker

CentOS 7.x

Before you install a new version of Docker, uninstall all existing Docker components. This prevents potential conflicts and compatibility issues.

Uninstall old versions of Docker

  1. Uninstall old versions of Docker, related packages, and software sources.

    You can ignore any messages that a package is not installed. This indicates that the package was not present.
    # Delete 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 images, containers, persistent volumes (PVs), or networks. This data is stored in the /var/lib/docker/ directory by default. You must delete this directory manually.

  1. Install Docker Community Edition.

    If you are not using an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://mirrors.aliyun.com
    # Add the Docker package source
    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
    # Set the Docker daemon to start automatically on system boot
    sudo systemctl enable docker

CentOS 8.x

Before you install a new version of Docker, uninstall all existing Docker components. This prevents potential conflicts and compatibility issues.

Uninstall old versions of Docker

  1. Uninstall old versions of Docker, related packages, and software sources.

    You can ignore any messages that a package is not installed. This indicates that the package was not present.
    # Delete Docker-related sources
    sudo rm -f /etc/yum.repos.d/docker*.repo
    # Uninstall Docker and related 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 images, containers, persistent volumes (PVs), or networks. This data is stored in the /var/lib/docker/ directory by default. You must delete this directory manually.

  1. Install Docker Community Edition.

    If you are not using an Alibaba Cloud server, replace http://mirrors.cloud.aliyuncs.com with https://mirrors.aliyun.com
    # Add the Docker package source
    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
    # Set the Docker daemon to start automatically on system boot
    sudo systemctl enable docker

Configure image source

Pulling images from Docker Hub might fail because of ISP network issues. You can configure an image accelerator to resolve this issue.

Important

The docker search command fails to execute because it directly queries Docker Hub.

  1. Log on to the Container Registry console. In the navigation pane on the left, choose Image Tool > Image Accelerator. On the Image Accelerator page, obtain the Booster URL.

  2. Edit the Docker configuration file /etc/docker/daemon.json. If the file does not exist, create it.

    {
        "registry-mirrors": ["<Accelerator Address>"]
    }            
  3. Restart the Docker service to apply the new configuration.

    sudo systemctl restart docker

Use Docker to quickly deploy a web app

  1. Build the web application image.

    # Pull the Nginx image
    sudo docker pull nginx:latest
    # Create a Dockerfile that sets Nginx as the base image and creates an index.html file in the web server's root directory to display "Hello World!".
    sudo tee Dockerfile <<-'EOF'
    FROM nginx:latest
    RUN echo 'Hello World!' > /usr/share/nginx/html/index.html
    EOF
    # Build the image and name it hello-world
    sudo docker build . -t hello-world:latest
  2. Use the web application image to start a container named hello-world.

    sudo docker run -d -p 80:80 --name hello-world hello-world:latest
  3. Run the curl http://localhost command to verify that the web application correctly displays Hello World!.

Install Docker Compose

When an application contains multiple services or has dependencies on the startup order between services, you can use Docker Compose to define, run, and manage them together using a single YAML file.

Alibaba Cloud Linux, CentOS, Red Hat, Anolis, and Fedora

sudo yum -y install docker-compose-plugin

Ubuntu and Debian

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

Use Docker Compose to deploy an application

  1. Create the orchestration file docker-compose.yaml.

    # Create the orchestration file and add MySQL and WordPress
    sudo tee docker-compose.yaml <<-'EOF'
    version: '3.1'
    
    services:
      wordpress:
        image: wordpress
        restart: always
        ports:
          - "80:80"
        environment:
          # Database 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.

    When you use Docker Compose V2, the command does not include a hyphen. For example, you can run docker compose version.
    # Start the service container.
    sudo env "PATH=$PATH" "WORDPRESS_DB_PASSWORD=<database_password>" docker compose -f docker-compose.yaml up -d
  3. In a browser, enter http://<Public IP of your ECS instance> to access WordPress.

    You must allow traffic on port 80 in the security group

FAQ

Still unable to pull images after configuring the image accelerator

Because of unstable factors such as ISP networks, the image accelerator cannot guarantee that a specific version of a container image can be pulled successfully. We recommend that you reduce dependencies on Docker Hub container images in production environments. If the pull fails, see Official image acceleration.

Configure non-root user permissions for Docker

By default, Docker commands require root permissions (using sudo). To avoid typing sudo every time and to follow the principle of least privilege, add the current user to the docker user group.

  1. Add the current user to the docker group.

    sudo usermod -aG docker $USER
  2. Run the newgrp docker command for the changes to take effect. You can then use docker commands directly without sudo.

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

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

  • Possible cause: Unstable network.

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

    Using https://mirrors.aliyun.com requires your instance to have an Internet connection. If you are unsure how to enable an Internet connection, see Enable an Internet connection.

Error when running the dnf config-manager command

  • Symptom: The following error occurs when you run the dnf config-manager command on a Linux instance:

    /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.
  • Possible cause: The dnf version is too old, which causes the installation to fail.

  • Solution: Run the `sudo dnf update dnf` command to update the dnf package to the latest version, and then try to install Docker again.

Error when running the dnf install docker-ce command

  • Symptom: The following error occurs when you run the dnf -y install docker-ce command on a Linux instance:

    (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.
  • Possible cause: The package cache is too old, which causes the installation to fail.

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

  • Symptom: The following error occurs when you run the dnf -y install docker-ce command on a Linux instance:

    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
  • Possible cause: You are using a CentOS 8 software source that has been taken offline.

  • Solution: Follow these steps to replace the offline CentOS 8 software source with an available source provided by Alibaba Cloud.

    # Back up the expired software source.
    sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    # Download the CentOS-Base.repo file provided by Alibaba Cloud 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
    # Replace the software source address in the Alibaba Cloud source with a source address accessible to ECS.
    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

    After you complete the preceding steps, try to install docker again.

References