All Products
Search
Document Center

Elastic Compute Service:Install and use Docker

Last Updated:Sep 05, 2024

You can use Docker repositories provided by Alibaba Cloud to quickly install Docker on Elastic Compute Service (ECS) instances. This topic describes how to install Docker on Linux ECS instances, how to use Docker to create images, and how to use Docker Compose to streamline the process of building, deploying, and managing applications.

Install Docker

Prerequisites

An ECS instance that meets the following requirements is created. For information about how to create an ECS instance, see Create an instance on the Custom Launch tab.

  • Operating system: Alibaba Cloud Linux 3 64-bit, Alibaba Cloud Linux 2 64-bit, CentOS 7.x 64-bit, CentOS 8.x 64-bit, Ubuntu 22, Ubuntu 20, Debian 11, or Debian 12.

  • Network type: Virtual Private Cloud (VPC).

  • IP address: The instance is assigned a static public IP address (also known as auto-assigned or system-assigned public IP address) or associated with an elastic IP address (EIP). For information about how to assign a static public IP address to or associate an EIP with an ECS instance, see Enable public bandwidth for an ECS instance.

  • Security group: Inbound rules are added to a security group to which the instance belongs to allow traffic on ports 22 and 80. For information about how to add an inbound security group rule, see Add a security group rule.

Procedure

  1. Connect to an ECS instance.

    For more information, see Connect to a Linux instance by using a password or key.

  2. Install Docker.

    Alibaba Cloud Linux 3

    1. Run the following command to add the Dandified YUM (DNF) repository of Docker Community Edition (Docker-CE):

      sudo dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    2. Run the following command to install the DNF repository plug-in that is dedicated to Alibaba Cloud Linux 3:

      sudo dnf -y install dnf-plugin-releasever-adapter --repo alinux3-plus
    3. Run the following command to install Docker:

      sudo dnf -y install docker-ce --nobest
      Note

      For information about how to resolve Docker installation issues, see the FAQ section of this topic.

    Alibaba Cloud Linux 2

    1. Run the following command to download the YUM repository:

      sudo wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    2. Run the following command to install the YUM repository plug-in that is dedicated to Alibaba Cloud Linux 2:

      sudo yum install yum-plugin-releasever-adapter --disablerepo=* --enablerepo=plus
    3. Run the following command to install Docker:

      sudo yum -y install docker-ce

    CentOS 7.x

    1. Run the following command to download the YUM repository:

      sudo wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    2. Run the following command to install Docker:

      sudo yum -y install docker-ce

    CentOS 8.x

    1. Change the CentOS 8 repository address.

      CentOS 8 reached EOL. In accordance with Linux community rules, all content was removed from the following CentOS 8 repository address: http://mirror.centos.org/centos/8/. If you continue to use the default CentOS 8 repository on Alibaba Cloud, an error is reported. To use specific installation packages of CentOS 8, change the CentOS 8 repository address. For more information, see Change CentOS 8 repository addresses.

    2. Run the following command to install DNF:

      sudo yum -y install dnf
    3. Run the following command to install the dependency package of the Docker storage driver:

      sudo dnf install -y device-mapper-persistent-data lvm2
    4. Run the following command to add a stable Docker repository: S

      sudo dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    5. Run the following command to check whether the Docker repository is added:

      sudo dnf list docker-ce

      The following command output indicates that the Docker repository is added.

      image..png

    6. Run the following command to install Docker:

      sudo dnf install -y docker-ce --nobest

    Ubuntu 18, Ubuntu 22, Ubuntu 20, Debian 11, or Debian 12

    1. Run the following command to update the software package list:

      sudo apt update
    2. Run the following command to install the dependencies that are required by Docker.

      sudo apt-get -y install ca-certificates curl
    3. Create the /etc/apt/keyrings directory and download the official GNU Privacy Guard (GPG) key of Docker to the directory.

      Ubuntu 18, Ubuntu 20, or Ubuntu 22

      sudo install -m 0755 -d /etc/apt/keyrings
      sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
      sudo chmod a+r /etc/apt/keyrings/docker.asc

      Debian 11 or Debian 12

      sudo install -m 0755 -d /etc/apt/keyrings
      sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
      sudo chmod a+r /etc/apt/keyrings/docker.asc
    4. Add the Docker repository to the software repository list of the system.

      Ubuntu 18, Ubuntu 20, or Ubuntu 22

      echo \
        "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] http://mirrors.aliyun.com/docker-ce/linux/ubuntu \
        $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
        sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

      Debian 11 or Debian 12

      echo \
        "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] http://mirrors.aliyun.com/docker-ce/linux/debian \
        $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
        sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    5. Run the following command to update the software package list:

      sudo apt update
    6. Run the following command to install Docker:

      sudo apt-get install -y docker-ce docker-ce-cli containerd.io
  3. Check the version of Docker.

    docker -v

    The following command output indicates that Docker is installed.

    image..png

  4. Start the Docker daemon and configure the Docker daemon to start on system startup.

    • Run the following commands to start Docker and configure Docker to start on system startup:

      sudo systemctl start docker
      sudo systemctl enable docker
    • Run the following command to check whether Docker is started:

      sudo systemctl status docker

      The following command output indicates that Docker is started.

      image..png

Basic Docker operations

This section describes only basic Docker usage. For information about more Docker usage, visit the official Docker website.

  • Run the following commands to manage the Docker daemon.

    sudo systemctl start docker # Run the Docker daemon.
    sudo systemctl stop docker      # Stop the Docker daemon.
    sudo systemctl restart docker   # Restart the Docker daemon.
    sudo systemctl enable docker   # Configure the Docker daemon to start on system startup.
    sudo systemctl status docker # Check the status of the Docker daemon.
  • Manage images

    In this example, Apache images from Alibaba Cloud Container Registry are used to describe how to use Docker to manage images.

    • Pull an image.

      sudo docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
    • Modify tags. The names of images from Alibaba Cloud Container Registry are long. Use tags to easily identify the images.

      sudo docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1
    • View existing images.

      sudo docker images
    • Forcefully delete an image.

      sudo docker rmi -f registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
  • Manage containers

    Important
    • To query the <Image ID> value for use in the following code snippets, run the docker images command.

    • Before you run commands in a container, run the cat /etc/os-release command in the container to view the operating system type and version. Then, determine the commands that are suited to the operating system type and version.

    • Start a container.

      You can start a container in daemon or interactive mode.

      Note
      • If a container is started in daemon mode, the container remains in the Running state after you exit the container.

      • If a container is started in interactive mode, the status of the container changes to Exited after you exit the container. To resume the container, run the sudo docker start <Container ID> command.

      Start a container in daemon mode

      sudo docker run -d --name <Container name> <Image ID>

      Start a container in interactive mode

      # Start a new container in interactive mode. 
      sudo docker run -it <Image ID> /bin/bash 
    • Query container IDs.

      sudo docker ps -a
    • Start a container in the Stopped state.

      sudo docker start <Container ID>
    • Run a command in a running container. Example:

      sudo docker exec -it <Container ID> /bin/bash
      Note

      To exit the container, run the exit command.

Use Docker to create an image

This section describes how to create a simple custom NGINX image from a Dockerfile.

  1. Run the following command to pull an image. In this example, an Apache image from Alibaba Cloud Container Registry is pulled.

    sudo docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
  2. Modify the tag of the image to easily identify the image.

    sudo docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1
  3. Create and edit a Dockerfile.

    1. Run the following command to create and edit a Dockerfile:

      vim Dockerfile
    2. Press the I key to enter Insert mode and add the following content to the file:

      #Specify a base image. 
      FROM aliweb:v1
      #Specify the owner of the base image. 
      MAINTAINER DTSTACK
      #Specify the command that you want to run before the container starts. Append the command to the end of the RUN command. To run many commands, we recommend that you write the commands to a script. Each Docker image can contain up to 127 layers. 
      RUN mkdir /dtstact
      #Specify the commands that are run on system startup. The last command must be a frontend command that constantly runs. This prevents the container from immediately exiting after starting up. 
      ENTRYPOINT ping www.aliyun.com
    3. Press the Esc key, enter :wq, and then press the Enter key to save and close the Dockerfile.

  4. Run the following command to create an image based on the base NGINX image.

    Run the command in the following format: docker build -t <Image name>:<Image version>.. The period (.) at the end of the command indicates the directory of the Dockerfile. The period (.) is required. For example, run the following command to build an image named aliweb in version v2:

    sudo docker build -t aliweb:v2 .
  5. Run the following command to check whether the image is built:

    sudo docker images 

    The following command output indicates that the image is built.

    image..png

Install and use Docker Compose

Docker Compose is an open source container orchestration tool developed by the Docker team and used to define and run multiple containers. In Docker Compose, you use a YAML file to configure the services of your application, run a command to parse the configurations of the YAML file, and then create and start all services from the configurations. Docker Compose helps reduce O&M costs and improve deployment efficiency.

For more information about Docker Compose, visit the official Docker website.

Important

Docker Compose is supported only in Python 3 and later. Make sure that pip is installed.

Install Docker Compose

  1. Run the following command to install setuptools:

    sudo pip3 install -U pip setuptools
  2. Run the following command to install Docker Compose:

    sudo pip3 install docker-compose
  3. Run the following command to check whether Docker Compose is installed:

    docker-compose --version

    If the version of Docker Compose is returned, Docker Compose is installed.

Use Docker Compose to deploy an application

This section describes how to use Docker Compose to deploy WordPress.

  1. Create and edit the docker-compose.yaml file.

    1. Run the following command to create the docker-compose.yaml file:

      sudo vim docker-compose.yaml
    2. Press the I key to enter Insert mode and add the following content to the file.

      In this example, the content that is used to install WordPress is added.

      version: '3.1'             # Specify the version of Docker Compose.
      
      services:
      
        wordpress:               # Specify a service name.         
          image: wordpress       # Specify an image name.
          restart: always        # Configure the container to start each time Docker starts.
          ports:
            - 80:80              # Specify a port mapping.
          environment:           # Configure environment variables.
            WORDPRESS_DB_HOST: db
            WORDPRESS_DB_USER: wordpress
            WORDPRESS_DB_PASSWORD: 123456
            WORDPRESS_DB_NAME: wordpress
          volumes:               # Configure mappings between containers and ECS volumes.
            - wordpress:/var/www/html
      
        db:                      # Specify a service name.    
          image: mysql:5.7       # Specify an image name.
          restart: always        # Configure the container to start each time Docker starts.
          ports:
             - 3306:3306         # Specify a port mapping.
          environment:           # Configure environment variables.
            MYSQL_DATABASE: wordpress
            MYSQL_USER: wordpress
            MYSQL_PASSWORD: 123456
            MYSQL_RANDOM_ROOT_PASSWORD: '1'
          volumes:               # Configure mappings between containers and ECS volumes.
            - db:/var/lib/mysql
      
      volumes:
        wordpress:
        db:
    3. Press the Esc key to exit Insert mode, enter :wq, and then press the Enter key to save and close the file.

  2. Run the following command to start the application:

    sudo env "PATH=$PATH" docker-compose up -d
  3. Enter an address in the http://<Public IP address of the ECS instance> format in the address bar of your browser to go to the WordPress configuration page. On the page, you can configure the parameters as prompted to access WordPress.

FAQ

What do I do if errors occur when I run systemctl commands on a Linux ECS instance or on a container in a Linux ECS instance?

  • Problem description: When you run systemctl commands on a Linux ECS instance or on a container in a Linux ECS instance, the following error messages appear:

    • System has not been booted with systemd as init system (PID 1). Can't operate.

    • Failed to get D-Bus connection: xxx xxx.

  • Cause: The systemd process is not used to initialize the system (init system).

  • Solution: Run service commands to manage services. For example, run the sudo service sshd restar command instead of the sudo systemctl restart sshd.service command.

What do I do if an error occurs when I run the dnf config-manager command on a Linux ECS instance?

  • Problem description: When you run the dnf config-manager command on a Linux ECS instance, the following error message appears:

    • /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 version is outdated.

  • Solution: Run the sudo dnf update dnf command to update the DNF package to the latest version and re-install Docker.

What do I do if an error occurs when I run the dnf install docker-ce command on a Linux ECS instance?

Problem description 1: When you run the dnf -y install docker-ce command on a Linux ECS instance, the following error message appears:

(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 installation package in the cache is outdated.

Solution: Run the sudo dnf clean packages command to clear the installation package in the cache and re-install Docker.

Problem description 2: When you run the dnf -y install docker-ce command on a Linux ECS instance, the following error messages appear:

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: An expired CentOS 8 repository is used.

Solution: Perform the following operations to replace the expired CentOS 8 repository with an available repository provided by Alibaba Cloud:

# Back up the expired repository. 
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 the /etc/yum.repos.d/ directory. 
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.aliyuncs.com/repo/Centos-vault-8.5.2111.repo
# Replace the repository addresses in the Alibaba Cloud repository with repository addresses that are accessible to the ECS instance. 
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 existing installation package in the cache and generate a new installation package in the cache based on the repository that you specified. 
sudo yum clean all && sudo yum makecache

Re-install Docker after you perform the preceding operations.

Note

If you do not want to use the repository provided by Alibaba Cloud, comment out the unavailable repository addresses in the /etc/yum.repos.d/CentOS-Base.repo file and re-install Docker.

References

  • For information about how to use Docker, see Docker documentation.

  • Use Docker images.

    Alibaba Cloud Container Registry releases Artifact Center to provide container developers with secure and trusted base container images from the Alibaba Cloud website and OpenAnolis community free of charge. After you install Docker, you can use the Docker container images in Artifact Center to meet specific business requirements, such as application deployment, environment development, operating system setup, and AI or big data learning framework construction.

  • Accelerate Docker image pulling.

    You can use the P2P acceleration feature to accelerate image pulling and application deployment. 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.

  • You can also use Alibaba Cloud Container Registry to run and manage containerized applications.