Service orchestration of docker

docker service orchestration

  • Docker service orchestration, also known as docker compose, is designed to solve the problem of deploying large-scale applications using docker. Generally, in the production environment, using docker to deploy an application requires multiple containers, such as deploying front-end, back-end, dependent services, databases, etc. If each container needs to be started manually with a shell, the efficiency is too low. Therefore, in such a scenario, we can use docker compose to arrange the docker container.

Step 1: Download docker compose

curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

Step 2: modify docker compose to allow it to execute

chmod 744 /usr/local/bin/docker-compose

Step 3: Test

docker-compose -version

Part IV: docker compose Compilation of YML document
At docker compose Relevant configuration attributes can be written in the YML file to manage the build process of compose. The main attributes include:
1) Version stands for the version of compose file format. According to the example on the official website, it is recommended to adopt the '3.9' version at present
2) Services this attribute is the core attribute in the compose file. Under this attribute, you can configure various services started by compose. It mainly refers to various container information that needs to be started by compose. It is also the main development content in the compose file.
Common attributes:

Attribute nameexplain
imageWhat image file does the current service use
portsPort information that the current container needs to map - "8080:80"“
enviromentsParameters to be passed in the current image
volumesHanging point of the current mirror image
tty: trueThe representative starts the service using - it mode
buildSpecify the path where the dockerfile is located. This parameter is required when arranging a container with docker compose and the container needs to be built
container_nameSpecifies the name of the orchestration container
restart: alwaysDoes the container restart automatically after the docker service is restarted
depends_on: service nameSet the service dependency, and the startup order of the service can be determined through this configuration
env_fileSpecify profile
contextSpecify context
dockerfileSpecify the dockerfile file to use

3)networks

Example 1: configure simple service information in the service, write a compose file, and start an nginx container

version: '3.9'
services:
  my_nginx:
    image: "nginx"
    ports:
      - "8080:9090"
    volumes:
      - "/root/docker_vol/web/dist:/usr/share/nginx/html:ro"
      - "/root/docker_vol/web/conf/nginx.conf:/etc/nginx/nginx.conf:ro"
  • Use the docker compose up command to start the container orchestration. It should be noted that after the container is successfully started, the current terminal will be occupied by default to output log information. We can reopen a terminal and use the docker ps command to check. We find that there is an additional container named * * my in docker_ nginx_ 1. This is the container that compose started for us.
  • In the above example, my_nginx is the name of a service, which can also be understood as a part of the container name. The image attribute represents what image to use, ports represents the port to be mapped, the port of the host is in front of it, and the port in the container is behind it. Columns represents the file mount to be performed.
  • In addition, using the docker network ls command, it can be found that after composition is started, a network is automatically created for us. Through the docker inspect command, it is found that all containers started with the current composition file are added to the newly created network by default.

Example 2: build a service in the Tomcat compose file, and there is no corresponding image in the local docker

version: '3.9'

services:
  my_tomcat:
    image: "tomcat"
  • If the image that does not exist in the local docker is included in the compose, the corresponding image will be automatically downloaded during the operation of compose. Theoretically, docker and docker are installed on one computer
  • On the host of compose, on the premise of ensuring the smooth network, we can directly install the corresponding system by relying on the docker compose file.
  • In addition, in the current example, we modify what the compose file considers to be
    tomcat-compose.yml. If the default file name is not used, you need to add the - f parameter when executing the docker compose up command to manually specify the corresponding yml file.

Example 3: start multiple containers at once

version: '3.9'

services:
  my_tomcat:
    image: "tomcat"
  my_nginx:
    image: "nginx"
    ports:
      - "8080:9090"
    volumes:
      - "/root/docker_vol/web/dist:/usr/share/nginx/html:ro"
      - "/root/docker_vol/web/conf/nginx.conf:/etc/nginx/nginx.conf:ro"

Docker compose command

In the previous example, we used the docker compose up command to start a container orchestration. Next, let's learn more about the commonly used commands and parameters in docker compose

  1. Docker compose command
    Common parameters:
    -F by default, dockercompose will inquire about dockercompose in the current folder YML file. If you need to customize the file name, you can specify it in the form of - f file name
    -p specify the project name. After specifying the project, all containers created through compoose will be automatically prefixed with the project name
    -v view the version information of the current docker compose
  2. Docker compose environment variable
    The default environment variable of docker compose is Env, in which we can use the corresponding key value pair mode to pass the value to compose -- env file parameter can dynamically brake the location of the configuration file that compose needs to use
  3. Docker compose up starts a container orchestration
    Common parameters:`
Parameter nameexplain
-dBackground operation
–force-recreateForce update of existing containers
–remove-orphansDelete containers that are not defined in the compose file. It is usually used to use the service list to start a service in compose alone in the scene where the compose file is modified. If the service has a dependency, the dependent service will also be started
docker-compose downDelete the corresponding container, network service
docker-compose stopStop all services corresponding to compose
docker-compose startStart all service containers
docker-compose psView the service container related to the current compse
-qOnly id values are listed, and other values are not displayed
docker-compose logsRead log information
docker-compose pullPull the image corresponding to the service
docker-compose buildBuild all images in compose
docker-compose imagesView all image information
docker-compose configView profile information
docker-compose restartRestart service
Docker compose exec service nameThe container that performs the specified service
docker-compose rmDelete all stopped service containers
When the above command involves a service, you can operate the corresponding service through the command: service nameLook to the left

docker compose network management

  1. In the default scenario, the corresponding network will be automatically created by using docker compose to arrange the service. You can use docker network ls
    And docker inspect network id.
  2. You can use networks in the compose file
    Define a network and join a custom network in the service using the networks attribute. At this time, there are three results. If all services are added to the custom network, docker will create the custom network and add all service containers to the corresponding network. If no service declaration is added to the custom network, docker will create the default network and then add the container to the default network instead of creating the custom network, If some service declarations are added to the custom network and some services are not declared, docker will create two networks, one is the custom network and the other is the default network, and add the corresponding container to the corresponding network.
  3. Use fixed ip Policy
version: '3.3'
services:
  cslcp_web:
    image: "cslcp_web_image:v01"
    ports:
      - "19090:8080"
    build:
      context: "../web/"
      dockerfile: "Dockerfile"
    networks:
      - "cslcp_net"
  cslcp_mysql:
    image: "mysql"
    networks:
      cslcp_net:
        ipv4_address: 192.168.13.100
    ports:
      - "13306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: 'root'
  cslcp_centos:
    container_name: 'test_centos'
    image: "centos"
    tty: true
    networks:
      cslcp_net:
        ipv4_address: 192.168.13.11
networks:
  cslcp_net:
    ipam:
      config:
        - subnet: "192.168.13.0/24"
          gateway: "192.168.13.1"
  1. Using service policies
    When compose is started successfully, you can directly access the service name by ping
  2. Use network alias policy
networks:
  cslcp_net:
    ipam:
      config:
      - gateway: 192.168.13.1
        subnet: 192.168.13.0/24
services:
  cslcp_centos:
    container_name: test_centos
    depends_on:
      cslcp_web:
        condition: service_started
    image: centos
    links:
    - cslcp_web:cw
    networks:
      cslcp_net:
        ipv4_address: 192.168.13.11
    tty: true
  cslcp_mysql:
    environment:
      MYSQL_ROOT_PASSWORD: root
    image: mysql
    networks:
      cslcp_net:
        ipv4_address: 192.168.13.100
    ports:
    - published: 13306
      target: 33064
  cslcp_web:
    build:
      context: /root/docker/dockercompose/cslcp/web
      dockerfile: Dockerfile
    image: cslcp_web_image:v01
    networks:
      cslcp_net: null
    ports:
    - published: 19090
      target: 8080
version: '3.3'
  1. Use container policy
version: '3.3'
services:
  cslcp_web:
    image: "cslcp_web_image:v01"
    build:
      context: "../web/"
      dockerfile: "Dockerfile"
    depends_on:
      - "cslcp_centos"
    network_mode: "service:cslcp_centos"
  cslcp_mysql:
    image: "mysql"
    environment:
      MYSQL_ROOT_PASSWORD: 'root'
    depends_on:
      - "cslcp_centos"
    network_mode: "service:cslcp_centos"
  cslcp_centos:
    container_name: 'test_centos'
    image: "centos"
    tty: true
    ports:
      - "17070:8080"
      - "13306:3306"
    networks:
      cslcp_net:
        ipv4_address: 192.168.13.11
networks:
  cslcp_net:
    ipam:
      config:
        - subnet: "192.168.13.0/24"
          gateway: "192.168.13.1"

It is not easy to make and sort out. The above contents are original (refer to some official documents and cases sorted out by teachers). If you want to quote, please attach a link to this article. If you have any questions, you can speak freely in the comment area. The author will reply as soon as he sees it. Welcome to communicate!

Keywords: Linux Operation & Maintenance CentOS Docker

Added by duklaprague on Sat, 29 Jan 2022 05:38:29 +0200