> For the complete documentation index, see [llms.txt](https://docker.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docker.shujuwajue.com/docker-ji-qun/docker-compose.md).

# Docker Compose

[官方文档：Docker-compose介绍](https://docs.docker.com/compose/overview/)

[官方文档：Docker-compose文件语法](https://docs.docker.com/compose/compose-file/)

[官方文档：Docker-compose命令](https://docs.docker.com/compose/reference/overview/)

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

## 安装

[官方安装文档](https://docs.docker.com/compose/install/)

```
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](https://docs.docker.com/compose/compose-file/)版本语法语法

## 操作

[官方文档：Docker-compose命令](https://legacy.gitbook.com/book/xiaoxiami/docker/edit#)

启动所有

```
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重启策略](http://www.voidcn.com/article/p-gthqvcdm-bte.html)
