Help Center> Elastic Cloud Server> Best Practices> Manually Deploying Docker (CentOS 7.5)
Updated on 2023-11-13 GMT+08:00

Manually Deploying Docker (CentOS 7.5)

Overview

The best practices for Huawei Cloud ECS guide you through the manual deployment of Docker on a Linux ECS. Additionally, common Docker operations and the process of creating a Docker image are provided.

Table 1 Docker terminologies

Term

Description

Docker

Docker is a platform for developers and system administrators to develop, deploy, and run applications using containers.

Docker image

Docker image is a special file system, which provides the programs, libraries, resources, and configuration files required for running containers. A Docker image also contains configuration parameters, for example, for anonymous disks, environment variables, and users. A Docker image does not contain any dynamic data, and its content remains unchanged after being built.

Container

Images become containers at runtime, that is, containers are created from images. A container can be created, started, stopped, deleted, and suspended.

For more information about Docker, image, and container, see Docker Documentation.

Docker requires 64bit OSs with a kernel version being 3.10 or later. This section uses CentOS 7.5 64 3.10.0-862.9.1.el7.x86_64 as an example.

Prerequisites

  • The target ECS has an EIP bound. For instructions about how to bind an EIP to an ECS, see Assigning an EIP and Binding It to an ECS.
  • The rule listed in the following table has been added to the security group to which the target ECS belongs. For details, see Adding a Security Group Rule.
    Table 2 Security group rule

    Transfer Direction

    Type

    Protocol

    Port/Range

    Remote End

    Inbound

    IPv4

    TCP

    80

    0.0.0.0/0

Deploying Docker

  1. Log in to the ECS.
  2. To obtain and update the system and software, update the image source to a Huawei Cloud image source. For details, see How Can I Use an EPEL Image Source (x86_64 or Arm) Provided by Huawei Cloud?
  3. Add a yum repository.

    yum install epel-release -y

    yum clean all

  4. Install yum-utils.

    sudo yum install -y yum-utils device-mapper-persistent-data lvm2

  5. Configure the yum repository for Docker.

    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

  6. Install and run Docker.

    sudo yum install docker-ce

    systemctl enable docker

    systemctl start docker

  7. Check the installation.

    docker --version

    If the information similar to the following is displayed, Docker has been installed:

    Client: Docker Engine - Community
     Version:           19.03.13

Basic Operations on Docker

  1. Managing Docker processes
    • Start Docker.

      systemctl start docker

    • Stop Docker.

      systemctl stop docker

    • Restart Docker.

      systemctl restart docker

  2. Managing Docker images
    1. Pull docker images, taking official Apache and CentOS images as an example.

      docker pull httpd

      docker pull centos

    2. View existing images.

      docker images

    3. Forcibly delete an image.

      docker rmi centos

  3. Managing containers
    1. Create a container and run it.

      docker run -it -d -p 80:80 --name datahttpd -v /data/:/var/www/httpd/ httpd

      The parameters are as follows:

      • -i: runs the container in interactive mode, which is usually used with -t.
      • -t: reallocates a pseudo input terminal to the container. This parameter is usually used with -i.
      • -d: runs the container at the backend and returns the container ID.
      • -p: port mapping, in the format of "Host port:Container port".
      • --name: specifies a name for the container.
      • -v: mounts an absolute directory on the host to the image, in the format of "Directory on the host:Mount path in the image".

      In the preceding parameters, the host is the target ECS.

      For example, use image httpd to start a container in interactive mode, map port 80 on the container to port 80 on the host, and map /data on the host to /var/www/httpd on the container, and have the container ID returned. Then, run the following command:

    2. Check whether the container has been started.

      docker ps -a

    3. In the address bar of the browser, enter the EIP bound to the ECS and check the running status of the container. If the following information is displayed, the container is running properly.

Creating an Image

Use Dockerfile to customize a simple Nginx image.

  1. Create a file named Dockerfile.

    mkdir mynginx

    cd mynginx

    touch Dockerfile

  2. Edit the file.

    vim Dockerfile

    Add the following data to Dockerfile:

    FROM nginx
    RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html

    Simple Dockerfile commands are as follows (for more information, log in at https://docs.docker.com/):

    • FROM statement: mandatory and must be the first instruction in Dockerfile, indicating that the Nginx image is used as a basic image.
    • RUN statement: indicates that the echo command is executed with the message "Hello, Docker!" displayed on the screen.
  3. Build the image.

    docker build -t nginx:v3 .

    • -t nginx:v3: specifies the image name and version.
    • .: specifies the context path. After the image-built command is executed, all data in the path will be packed to the Docker engine to build the image.
  4. Check the created Nginx image, the version of which is v3.

    docker images

    REPOSITORY          TAG             IMAGE ID              CREATED               SIZE
    nginx               v3              09422e465d96          10 seconds ago        109 MB