All Products
Search
Document Center

Simple Application Server:Deploy Docker

Last Updated:Sep 14, 2024

Docker is an open source containerization engine that provides tools and services for automatic packaging, distribution, and operation of containers. Docker provides good isolation, high portability, and simplified management. This topic describes how to deploy Docker on a simple application server, how to use Docker to create an image, and how to install and use Docker Compose to deploy WordPress. This topic also provides basic Docker operations.

Note
  • You can also use an application image to quickly deploy Docker.

    • If you did not create a simple application server, we recommend that you use an application image to deploy Docker. For more information, see Deploy Docker by using application images.

    • If you created a simple application server, you can replace the image of the server with a Docker application image to deploy Docker. The image replacement operation deletes all disk data from the simple application server. Before you replace the image of the simple application server, back up the disk data on the server based on your business requirements. For more information, see Reset a simple application server.

  • For information about Docker, see What is Docker?

Prerequisites

A Linux simple application server that meets the following requirements is created. For more information, see Create a simple application server.

  • 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.

  • Firewall: Rules are added to the simple application server to allow traffic on ports 22 and 80. For information about how to add a firewall rule, see the "Manage a firewall" section of the "Manage the firewall of a simple application server" topic.

Deploy Docker

  1. Connect to the simple application server. For more information, see Connect to a Linux server.

  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.

Manage the Docker daemon

  • Start the Docker daemon

    sudo systemctl start docker
  • Check the status of the Docker daemon

    sudo systemctl status docker
  • Stop the Docker daemon

    sudo systemctl stop docker
  • Restart the Docker daemon

    sudo systemctl restart docker
  • Enable the Docker daemon to start on server startup

    sudo systemctl enable docker

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 Docker Compose:

    sudo env "PATH=$PATH" docker-compose up -d
  3. Enter an address in the https://<Public IP address of the simple application server> 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 simple application server or on a container of a Linux simple application server?

  • 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.

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.