Home  >  Article  >  Operation and Maintenance  >  What you must know about Docker Compose (summary sharing)

What you must know about Docker Compose (summary sharing)

WBOY
WBOYforward
2022-02-02 06:00:323104browse

This article brings you relevant knowledge about Docker Compose, I hope it will be helpful to you.

Why do we need Docker Compose

Docker helps us solve the problem of packaging and installation of services. The problem that comes with it is that too many services bring about the following Problem,

  1. Use Dockerfile Build Image or DockerHub to pull Image multiple times;

  2. Need to create multiple Containers and write startup commands multiple times;

  3. How to manage and orchestrate Containers that depend on each other;

When the number of our services increases, the above three problems will become more serious Amplified, if these three problems are not solved, in fact, there seems to be no more changes from virtual machines to containerization except for reducing some waste of machines. Is there any good way in Docker that allows us to orchestrate and run containers through one configuration? At this time, Docker Compose came forward.

Docker Compose can do the following:

  1. Provide tools for defining and running multiple docker container applications;

  2. Use yaml files to configure application services (docker-compse.yml);

  3. You can use a simple command docker-compse up to start all services according to dependencies;

  4. You can stop all services with a simple command docker-compose down;

  5. When a service is needed, you can simply pass --scale Expansion;

Docker Compose has the following characteristics:

  1. Higher portability, Docker Compose only needs one docker-compse up Finish starting all services according to their dependencies, and then use docker-compose down to easily disassemble them. Help us deploy complex applications more easily;

  2. Multiple isolated environments on a single host, Compose can use project names to isolate environments from each other, which can be done on a single computer Running multiple copies of the same environment, it can prevent different projects and services from interfering with each other;

Introduction to Docker Compose

  1. Docker Compose is a Tools for defining and running multi-container applications;

  2. Docker Compose defines multi-container docker applications through yml files;

  3. Docker Compose uses one command to create or manage multiple containers based on the definition of the yml file;

    What you must know about Docker Compose (summary sharing)

Docker Compose is It is used for multi-container control of Docker and is a tool used to automate Docker. With Docker Compose, you can automate all complex Docker operations with just one command.

Docker Compose installation

The latest version of Docker Compose installation is 1.29.2. After installing Docker for Mac and Windows, Docker Compose is already installed. There is no need to install it manually. Here is the installation The method is based on Cnetos of Linux. You can also refer to the official website to install it.

The specific steps are as follows:

  1. Download the Docker Compose binary file, version 1.29.2 is The latest and most stable version at present. If you want to download the old version, you can change the version number. You can refer to the version number on github to choose;

    sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  2. Apply executable permissions to the binary file;

    sudo chmod +x /usr/local/bin/docker-compose
  3. After installation, check whether the installation is successful through the docker-compose --version command;

    What you must know about Docker Compose (summary sharing)

Introduction to Docker Compose versions

The corresponding relationship between Docker Compose versions and engines is as follows. You can see that there are two main formats, version 2 and version 3, which are currently used more often. These two, let me introduce to you the differences between these two versions:

  1. v3 version does not support volume_from, extends, group_add and other attributes;

  2. The settings of cpu and memory properties have been moved to deploy;

  3. The v3 version supports Docker Swarm, but the v2 version does not;

Note : The official has introduced a new --compatibility flag in 1.20.0 to help developers easily transition to v3. There are still some problems and the official does not recommend using it directly for production. It is recommended that everyone start the v3 version directly.

What you must know about Docker Compose (summary sharing)

Docker Compose basic command introduction

The Docker Compose command is basically similar to Docker, mainly for Docker Compose For life cycle control, log format and other related commands, you can use docker-compose --help for help.

#构建建启动nignx容器
docker-compose up -d nginx                     

#进入nginx容器中
docker-compose exec nginx bash            

#将会停止UP命令启动的容器,并删除容器
docker-compose down                             

#显示所有容器
docker-compose ps                                   

#重新启动nginx容器
docker-compose restart nginx                   

#构建镜像
docker-compose build nginx      

#不带缓存的构建
docker-compose build --no-cache nginx 

#查看nginx的日志
docker-compose logs  nginx                      

#查看nginx的实时日志
docker-compose logs -f nginx                   

#验证(docker-compose.yml)文件配置,
#当配置正确时,不输出任何内容,当文件配置错误,输出错误信息
docker-compose config  -q                        

#以json的形式输出nginx的docker日志
docker-compose events --json nginx       

#暂停nignx容器
docker-compose pause nginx                 

#恢复ningx容器
docker-compose unpause nginx             

#删除容器
docker-compose rm nginx                       

#停止nignx容器
docker-compose stop nginx                    

#启动nignx容器
docker-compose start nginx

Docker Compose actual combat

We build the following application and forward it to two Java applications on the backend through Nginx;

What you must know about Docker Compose (summary sharing)

  1. 新建Spring Boot应用,增加一个HelloController,编写一个hello方法,返回请求的端口和IP;

/**
 * hello
 *
 * @author wangtongzhou
 * @since 2021-07-25 09:43
 */
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(HttpServletRequest req) throws UnknownHostException {
        return "hello";
    }

}
  1. 指定Spring Boot的启动入口;

    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
                <configuration>
                    <!-- 指定该Main Class为全局的唯一入口 -->
                    <mainclass>cn.wheel.getway.WheelGetWay</mainclass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <!--可以把依赖的包都打包到生成的Jar包中-->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  1. 打包Spring Boot应用;

mvn package
  1. 上传文件到Linux服务器/usr/local/docker-compose-demo的目录;

  2. 在/usr/local/docker-compose-demo的目录编辑Dockerfile;

#指定基础镜像
FROM java:8
LABEL name="docker-compose-demo" version="1.0" author="wtz"
COPY ./getway-1.0-SNAPSHOT.jar ./docker-compose-demo.jar
#启动参数
CMD ["java","-jar","docker-compose-demo.jar"]
  1. 编辑docker-compose.yml文件;

version: '3.0'

networks:
  docker-compose-demo-net:
    driver: bridge
    ipam:
      config:
        - subnet: 192.168.1.0/24
          gateway: 192.168.1.1


services:
  docker-compose-demo01:
    build:
      #构建的地址
      context: /usr/local/docker-compose-demo
      dockerfile: Dockerfile
    image: docker-compose-demo
    container_name: docker-compose-demo01
    #选择网络
    networks:
      - docker-compose-demo-net
    #选择端口
    ports:
      - 8081:8080/tcp
    restart: always

  docker-compose-demo02:
    build:
      #构建的地址
      context: /usr/local/docker-compose-demo
      dockerfile: Dockerfile
    image: docker-compose-demo
    container_name: docker-compose-demo02
    #选择网络
    networks:
      - docker-compose-demo-net
    #选择端口
    ports:
      - 8082:8080/tcp
    restart: always

  nginx:
    image: nginx:latest
    container_name: nginx-demo
    networks:
      - docker-compose-demo-net
    ports:
      - 80:80/tcp
    restart: always
    volumes:
      - /usr/local/docker-compose-demo/nginx.conf:/etc/nginx/nginx.conf:rw


volumes:
  docker-compose-demo-volume: {}
  1. 编写nginx.conf,实现负载均衡到每个应用,这里通过容器名称访问,因此不需要管每个容器的ip是多少,这个也是自定义网络的好处;

user nginx;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;


    server {
        listen 80;
        location / {
         proxy_pass http://docker-compose-demo;
         proxy_set_header  Host $host;
		     proxy_set_header  X-real-ip $remote_addr;
		     proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

    upstream docker-compose-demo{
       server docker-compose-demo01:8080;
       server docker-compose-demo02:8080;
    }
    include /etc/nginx/conf.d/*.conf;
}
  1. 查看/usr/local/docker-compose-demo目录,有以下确保有以下四个文件;

What you must know about Docker Compose (summary sharing)

  1. 检查docker-compose.yml的语法是否正确,如果不发生报错,说明语法没有发生错误;

docker-compose config
  1. 启动docker-compose.yml定义的服务;

docker-compose up

What you must know about Docker Compose (summary sharing)

  1. 验证服务是否正确;

#查看宿主机ip
ip add

#访问对应的服务
curl http://172.21.122.231/hello

What you must know about Docker Compose (summary sharing)

What you must know about Docker Compose (summary sharing)

Docker Compose Yml文件介绍

版本

对于版本没什么介绍的,就是指定使用的版本;

Services

每个Service代表一个Container,与Docker一样,Container可以是从DockerHub中拉取到的镜像,也可以是本地Dockerfile Build的镜像。

image

标明image的ID,这个image ID可以是本地也可以是远程的,如果本地不存在,Docker Compose会尝试pull下来;

image: ubuntu

build

该参数指定Dockerfile文件的路径,Docker Compose会通过Dockerfile构建并生成镜像,然后使用该镜像;

build:
  #构建的地址
  context: /usr/local/docker-compose-demo
  dockerfile: Dockerfile

ports

暴露端口,指定宿主机到容器的端口映射,或者只指定容器的端口,则表示映射到主机上的随机端口,一般采用主机:容器的形式来映射端口;

#暴露端口
ports:
  - 8081:8080/tcp

expose

暴露端口,但不需要建立与宿主机的映射,只是会向链接的服务提供;

environment

加入环境变量,可以使用数组或者字典,只有一个key的环境变量可以在运行compose的机器上找到对应的值;

env_file

从一个文件中引入环境变量,该文件可以是一个单独的值或者一个列表,如果同时定义了environment,则environment中的环境变量会重写这些值;

depends_on

定义当前服务启动时,依赖的服务,当前服务会在依赖的服务启动后启动;

depends_on: 
  - docker-compose-demo02
  - docker-compose-demo01

deploy

该配置项在version 3里才引入,用于指定服务部署和运行时相关的参数;

replicas

指定副本数;

version: '3.4'
services:
  worker:
    image: nginx:latest
    deploy:
      replicas: 6

restart_policy

指定重启策略;

version: "3.4"
services:
  redis:
    image: redis:latest
    deploy:
      restart_policy:
        condition: on-failure   #重启条件:on-failure, none, any
        delay: 5s   # 等待多长时间尝试重启
        max_attempts: 3 #尝试的次数
        window: 120s    # 在决定重启是否成功之前等待多长时间

update_config

定义更新服务的方式,常用于滚动更新;

version: '3.4'
services:
  vote:
    image: docker-compose-demo
    depends_on:
      - redis
    deploy:
      replicas: 2
      update_config:
        parallelism: 2  # 一次更新2个容器
        delay: 10s  # 开始下一组更新之前,等待的时间
        failure_action:pause  # 如果更新失败,执行的动作:continue, rollback, pause,默认为pause
        max_failure_ratio: 20 # 在更新过程中容忍的失败率
        order: stop-first   # 更新时的操作顺序,停止优先(stop-first,先停止旧容器,再启动新容器)还是开始优先(start-first,先启动新容器,再停止旧容器),默认为停止优先,从version 3.4才引入该配置项

resources

限制服务资源;

version: '3.4'
services:
  redis:
    image: redis:alpine
    deploy:
      resources:
        #限制CPU的使用率为50%内存50M
        limits:
          cpus: '0.50'
          memory: 50M
        #始终保持25%的使用率内存20M
        reservations:
          cpus: '0.25'
          memory: 20M

healthcheck

执行健康检查;

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost"]   # 用于健康检查的指令
  interval: 1m30s   # 间隔时间
  timeout: 10s  # 超时时间
  retries: 3    # 重试次数
  start_period: 40s # 启动多久后开始检查

restart

重启策略;

#默认的重启策略,在任何情况下都不会重启容器
restart: "no"
#容器总是重新启动
restart: always
#退出代码指示失败错误,则该策略会重新启动容器
restart: on-failure
#重新启动容器,除非容器停止
restart: unless-stopped

networks

网络类型,可指定容器运行的网络类型;

#指定对应的网络
networks:
  - docker-compose-demo-net
  
  
networks:
  docker-compose-demo-net:
    driver: bridge
    ipam:
      config:
        - subnet: 192.168.1.0/24
          gateway: 192.168.1.1

ipv4_address, ipv6_address

加入网络时,为此服务指定容器的静态 IP 地址;

version: "3.9"

services:
  app:
    image: nginx:alpine
    networks:
      app_net:
        ipv4_address: 172.16.238.10
        ipv6_address: 2001:3984:3989::10

networks:
  app_net:
    ipam:
      driver: default
      config:
        - subnet: "172.16.238.0/24"
        - subnet: "2001:3984:3989::/64"

Networks

网络决定了服务之间以及服务和外界之间如何去通信,在执行docker-compose up的时候,docker会默认创建一个默认网络,创建的服务也会默认的使用这个默认网络。服务和服务之间,可以使用服务的名字进行通信,也可以自己创建网络,并将服务加入到这个网络之中,这样服务之间可以相互通信,而外界不能够与这个网络中的服务通信,可以保持隔离性。

Volumes

挂载主机路径或命名卷,指定为服务的子选项。可以将主机路径挂载为单个服务定义的一部分,无需在顶级volume中定义。如果想在多个服务中重用一个卷,则在顶级volumes key 中定义一个命名卷,将命名卷与服务一起使用。

推荐学习:《docker视频教程

The above is the detailed content of What you must know about Docker Compose (summary sharing). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete