This topic describes how to deploy Docker on CentOS.

Prerequisites

You must have an Alibaba Cloud account before you follow the instructions provided in the tutorial. To create an Alibaba Cloud account, click Create an Alibaba Cloud account.

Background information

The procedure described in this topic is applicable to developers that are familiar with Linux, but new to Alibaba Cloud ECS instances.

Procedure

To build Docker on a CentOS 7-based ECS instance, follow these steps:
  1. Deploy Docker
  2. Use Docker
  3. Create an image

Deploy Docker

You can purchase a required image from Alibaba Cloud Marketplace, and easily deploy Docker. You can also install Docker manually as described in this topic.

In the following example, the operating system version is CentOS 7.2 64 3.10.0-514.6.2.el7.x86_64.

Note Docker requires a 64-bit Linux system with Kernel 3.10 or later.

To deploy Docker, follow these steps:

  1. Add a Yellowdog Updater, Modified (YUM) repository.
    # yum install epel-release –y
    # yum clean all
    # yum list
  2. Install and run Docker.
    # yum install docker-io –y
    # systemctl start docker
  3. Check the installation result.
    # docker info
    The following response indicates that Docker has been installed.
    Installation result

Use Docker

You can use Docker in these ways:
  1. Manage the Docker daemon.
    # systemctl start docker     #Runs the Docker daemon.
    # systemctl stop docker      #Stops the Docker daemon.
    # systemctl restart docker   #Restarts the Docker daemon.
  2. Manage images. The following example uses Apache images from Alibaba Cloud image repository.
    # docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
    • Modify the tag of an image from Alibaba Cloud image repository to memorize the image easily.
      # docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1
    • Check existing images.
      # docker images
    • Delete an image.
      # docker rmi –f registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
  3. Manage containers.
    • Enter a container. Run the docker images command to obtain the image ID e1xxxxxxxxxe. Afterward, run the docker run command to enter the container corresponding the image ID.
      # docker run –ti e1xxxxxxxxxe /bin/bash
    • Exit the container. Run the exit command to exit the container.
    • You can combine the run command with the –d parameter to run the container in the background. The --name parameter specifies apache as the container name.
      # docker run -d --name apache e1xxxxxxxxxe
    • Enter the container that runs in the background.
      # docker exec -ti apache /bin/bash
    • Create an image from the container.
      # docker commit containerID/containerName newImageName:tag
    • To easily test and restore an image, you can run the source image, create a new image with a simple name from the source image, and then test the new image.
      # docker commit 4c8066cd8c01 apachephp:v1
    • Run the container and map Port 8080 of the host with the container.
      # docker run -d -p 8080:80 apachephp:v1

      In a browser, enter the IP address of the host followed by Port 8080 to connect to the container. The following response indicates that the container runs normally.


      Mapping result

Create an image

To create an image, follow these steps:

  1. Prepare the following content in a Dockerfile.
    # vim Dockerfile 
    FROM apachephp:v1  #Declares a base image.
    MAINTAINER DTSTACK #Declares the image owner.
    RUN mkdir /dtstact #The commands that you want to run before the container starts. You must add these commands to the end of the RUN command. The Dockerfile can only contain 127 lines or less. Therefore, we recommend that you write the required commands that you have not written in the Dockerfile to a script if the Dockerfile cannot contain these commands.
    ENTRYPOINT ping www.aliyun.com #The commands that run at startup. The last command must be a front-end command that runs constantly. Otherwise, the container will exit after running all commands.
  2. Build an image.
    docker build -t webcentos:v1 .   #The single dot (.) specifies the path of the Dockerfile and must be provided.
    docker images                    #Checks whether the image has been created.
    docker run –d webcentos:v1       #Runs the container in the background.
    docker ps                        #Queries the container in operation.
    docker ps –a                     #Queries all containers including those in the stopped state.
    docker logs CONTAINER ID/IMAGE   #Checks the startup log to troubleshoot the issue based on the container ID or name if the started container does not exist in the query result.
    docker commit fb2844b6c070 dtstackweb:v1 #The container ID and the name and version of the new image. You must add the information to the end of the commit command.
    docker images                    #Queries images that have been downloaded and created on the premises.
    docker push                      #Pushes an image to the default remote image repository, Docker Hub.
  3. Push the image to the registry.

    Enter your own ImageId and image version.

    docker login --username=dtstack_plus registry.cn-shanghai.aliyuncs.com #Specifies the password of the image repository. You enter the information after you run this command.
    docker tag [ImageId] registry.cn-shanghai.aliyuncs.com/dtstack123/test:[Image version]
    docker push registry.cn-shanghai.aliyuncs.com/dtstack123/test:[Image version]

    The image has been pushed to the registry if you can view the image version in the image repository.