Docker Compose

官方文档:Docker-compose介绍

官方文档:Docker-compose文件语法

官方文档:Docker-compose命令

Docker compose 是 Docker容器编排的工具, 可以配置并启动多个容器,适合复杂业务场景。

安装

官方安装文档

sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

然后赋予可执行权限

sudo chmod +x /usr/local/bin/docker-compose

查看下版本docker-compose --version

Docker cmpose 文件编写

文件名:docker-compose.yml

工具:yaml检查的在线工具http://www.yamllint.com/

目前可以使用compose file version 3版本语法语法

操作

官方文档:Docker-compose命令

启动所有

docker-compose up -d

-d 后台运行,使用docker-compose up --help查看更多

停止所有容器

docker-compose stop

也可以单独停止一个容器docker stop 容器名

删除多个容器

docker rm 容器1,容器2

自我提示示例:

services: 
  web1: 
    container_name: web1
    image: "centos:jdk"
    healthcheck:
      test: ["CMD", "curl","-s","-f", "http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3
    ports: 
      - "8080:80"
    privileged: true
    volumes: 
      - "/home/shenyi/nginx/web1/:/var/www/html/"
    networks:
      mynginx-net:
        ipv4_address: ${web1_addr}

  web2: 
    container_name: web2
    image: "centos:jdk"
    ports: 
      - "8081:80"
    privileged: true
    volumes: 
      - "/home/shenyi/nginx/web2/:/var/www/html/"
    networks:
      mynginx-net:
        ipv4_address: 192.156.0.3
    healthcheck:
      test: ["CMD", "curl","-s","-f", "http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3


  nginx:
    container_name: mynginx
    image: "centos:nginx"
    healthcheck:
      test: ["CMD", "curl","-s","-f", "http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3
    ports:
      - "9090:80"
    privileged: true
    volumes:
      - "/home/shenyi/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
    networks:
      mynginx-net:
        ipv4_address: 192.156.0.2
networks:
  mynginx-net:
    driver: bridge
    ipam: 
     config: 
      - subnet: 192.156.0.0/16
version: "3"

资料

Docker-Compose重启策略

Last updated