This topic describes how to deploy and use Docker on an Elastic Compute Service (ECS) instance that runs an Alibaba Cloud Linux 3.2104 64-bit operating system. This topic is intended for developers who are familiar with Linux but new to Alibaba Cloud ECS.

Prerequisites

One or more instances that run an Alibaba Cloud Linux 3.2104 64-bit operating system is created. For more information, see Create an instance by using the wizard.

In this topic, instances that have the following configurations are used:
  • Instance type: ecs.g6.large
  • Operating system: Alibaba Cloud Linux 3.2104 64-bit
  • Network type: Virtual Private Cloud (VPC)
  • IP address: public IP address

Background information

This topic describes the following operations:
  • Deploy Docker. For more information, see the Deploy Docker section.
  • Use Docker.
    • For information about how to use Docker, see the Use Docker section.
    • For information about how to create a Docker image, see the Create a Docker image section.

Deploy Docker

  1. Connect to an ECS instance.
    For more information about connection methods, see Connection methods.
  2. Run the following command to install Dandified YUM (DNF).
    DNF is the next-generation RPM package manager.
    yum -y install dnf
  3. Install Docker.
    You can use one of the following methods to install Docker:
    • Install the default Docker (podman-docker) in the DNF repository.
      1. Run the following command to install podman-docker:
        dnf -y install docker
      2. Run the following command to check whether Docker is installed:
        docker images
        A command output similar to the following one indicates that Docker is installed. podman-docker
        Notice podman-docker installed by using this method has no daemon (systemd). Therefore, you can use Docker without the need to pay attention to the running state of podman-docker in subsequent operations. You do not need to run the systemctl command to perform operations.
    • Install Docker Community Edition (Docker-CE).
      1. Run the following command to add the DNF repository of Docker-CE:
        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 for Alibaba Cloud Linux 3:
        dnf -y install dnf-plugin-releasever-adapter --repo alinux3-plus
      3. Run the following command to install Docker-CE:
        dnf -y install docker-ce --nobest
        If a command output similar to the following one is returned, you must comment out the CentOS repository in the /etc/yum.repos.d directory and then re-install Docker-CE. adad566
      4. Run the following command to check whether Docker-CE is installed:
        dnf list docker-ce
        A command output similar to the following one indicates that Docker-CE is installed. docker-ce
      5. Run the following command to start Docker:
        systemctl start docker
      6. Run the following command to check the running state of Docker.
        systemctl status docker
        A command output similar to the following one indicates that Docker is in the running state. docker-active
        Note To view the version of Docker, run the docker -v command.

Use Docker

Docker can be used in the following ways:
  • Manage the Docker daemon.
    systemctl start docker     #Run the Docker daemon.
    systemctl stop docker      #Stop the Docker daemon.
    systemctl restart docker   #Restart the Docker daemon.
    systemctl enable docker    #Configure Docker to run on system startup.
    systemctl status docker    #Check the running state of Docker.
    Note podman-docker installed by using the YUM repository has no daemon (systemd). Therefore, the systemctl command cannot be run to perform relevant operations.
  • Manage images. In the following example, an Apache image from Alibaba Cloud Container Registry is used:
    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 make the images easy to identify.
      docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1
    • View existing images.
      docker images
    • Forcefully delete an image.
      docker rmi -f registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
  • Manage containers.
    • Access the container. Run the docker images command to obtain the ImageId value, which is e1abc****. Then, run the docker run command to access the container.
      docker run -it e1abc**** /bin/bash
    • Exit the container. Run the exit command to exit the container.
    • Add the –d parameter to the docker run command to run the container in the background. Add the --name parameter to the command to specify apache as the container name.
      docker run -d --name apache e1abc****
    • Access the container that runs in the background.
      docker exec -it apache /bin/bash
    • Query the container ID.
      docker ps
    • Create an image from the container by using the following command syntax: docker commit <Container ID or container name> [<Repository name>[:<Tag>]].
      docker commit containerID/containerName repository:tag
    • For testing and restore purposes, run the image and derive a new image that has a simple name. Then, test the new image.
      docker commit 4c8066cd8**** apachephp:v1
    • Run the container and map port 8080 of the host on which the ECS instance resides to the container.
      docker run -d -p 8080:80 apachephp:v1
      In a browser, enter <IP address of the ECS instance>:8080 to connect to the container. A page similar to the one shown in the following figure indicates that the container runs normally.
      Note An inbound rule must be added to a security group of the ECS instance to allow inbound traffic on port 8080. For more information, see Add a security group rule.
      Mapping result

Create a Docker image

  1. Prepare Dockerfile.
    1. Create and edit Dockerfile.
      vim Dockerfile
    2. Press the I key to enter the edit mode and add the following content to the file:
      #Declare a base image. 
      FROM apachephp:v1
      #Declare the owner of the base image. 
      MAINTAINER DTSTACK
      #Specify the commands that you want to run before the container starts. You must append these commands to the end of the RUN command. Dockerfile can contain up to 127 lines. If the total length of your commands exceeds 127 lines, we recommend that you write these commands to a script. 
      RUN mkdir /dtstact
      #Specify the commands that are run on system startup. The last command must be a frontend command that runs constantly. Otherwise, the container exits after all the commands are run. 
      ENTRYPOINT ping www.aliyun.com
    3. Press the Esc key, enter :wq, and press the Enter key to save and exit Dockerfile.
  2. Create an image.
    docker build -t webalinux3:v1 .          #Use Dockerfile to create an image. . at the end of the command line specifies the path of Dockerfile and must be provided. 
    docker images                            #Check whether the image is created. 
  3. Run the container and check its state.
    docker run -d webalinux3:v1           #Run the container in the background. 
    docker ps                             #Query the containers that are in the running state. 
    docker ps -a                          #Query all containers, including those in the stopped state. 
    docker logs CONTAINER ID/IMAGE        #If the container does not appear in the query results, check the startup log to troubleshoot the issue based on the container ID or name. 
  4. Create an image.
    docker commit fb2844b6**** dtstackweb:v1     #Append the container ID and the name and version number of the new image to the end of the commit parameter. 
    docker images                                #Query images that are downloaded and created on premises. 
  5. Push the image to a remote repository.
    By default, the image is pushed to Docker Hub. You must log on to Docker, add a tag to the image, and then name the image in the <Docker username>/<Image name>:<Tag> format. Then, the image is pushed to the remote repository.
    docker login --username=dtstack_plus registry.cn-shanghai.aliyuncs.com    #Enter the password of the image repository after you run this command. 
    docker tag [ImageId] registry.cn-shanghai.aliyuncs.com/dtstack123/test:[Tag]
    docker push registry.cn-shanghai.aliyuncs.com/dtstack123/test:[Tag]