相关站点
Docker Compose 介绍
Docker Compose 项目来源于以前的 fig 项目,使用 Python 语言编写,是 Docker 官方推出的一款单机容器编排工具,与 Docker Swarm、Docker Machine 并称为 Docker 容器编排三剑客。其支持定义和运行多容器的应用,可以一条命令启动多个容器,使用 Docker Compose 后不再需要使用 Shell 脚本来启动容器。Docker Compose 通过一个配置文件来管理多个 Docker 容器,在配置文件中所有的容器通过 services
来定义,然后使用 docker-compose
命令来启动、停止、重启应用和应用中的服务以及所有依赖服务的容器,非常适合组合使用多个容器进行开发的场景。
安装 Docker Compose
Docker Compose 具体的版本号可以从 Docker Compose Releases 获取。
1 2 3 4 5 6 7 8 9 10 11
| # curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# sha256sum /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
# docker-compose version
|
安装 Docker Compose 的命令自动补全工具
1 2 3 4 5 6 7
| # yum install bash-completion
# curl -L https://raw.githubusercontent.com/docker/compose/1.29.2/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
|
Docker Compose 常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| # docker-compose pull
# docker-compose up
# docker-compose up -d
# docker-compose ps
# docker-compose start
# docker-compose stop
# docker-compose down
|
若 YAML 配置文件不在当前目录下,或者配置文件名不是 docker-compose.yml
、docker-compose.yaml
,那么则需要通过 -f
参数指定 YAML 配置文件的路径
1 2
| # docker-compose -f /example/nacos-standalone-mysql-5.7.yml pull
|
Docker Compose 配置文件编写示例
docker-compose.yml
配置文件的内容如下,主要定义了容器 zookeeper
与 dubbo-admin
,其中通过自定义网络(网桥)来指定每个容器的 IP 地址(静态 IP)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| version: "3.5"
services: zookeeper: image: clay/zookeeper-server:3.4.13 container_name: dubbo-zookeeper ports: - 2181:2181 networks: distributed-network: ipv4_address: 172.171.0.2 volumes: - '/container/zookeeper/log:/usr/local/zookeeper-3.4.13/log' - '/container/zookeeper/data:/usr/local/zookeeper-3.4.13/data' dubbo-admin: image: clay/dubbo-admin:0.1 container_name: dubbo-admin depends_on: - zookeeper networks: distributed-network: ipv4_address: 172.171.0.3 ports: - 8083:8080
networks: distributed-network: name: distributed-network driver: bridge ipam: config: - subnet: 172.171.0.0/24
|
Docker Compose 覆盖镜像中的 CMD 指令
1 2 3 4 5 6 7 8 9 10 11 12 13
| version: "3.5"
services: redis: image: redis:5.0.4-stretch container_name: redis-5.0.4 privileged: false ports: - 6379:6379 volumes: - '/container/redis/data:/data' - '/container/redis/redis.conf:/usr/local/etc/redis/redis.conf' command: redis-server /usr/local/etc/redis/redis.conf
|
Docker Compose 指定网络模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| version: "2"
services: nacos: image: nacos/nacos-server:latest container_name: nacos-standalone-mysql env_file: - /usr/nacos/env/nacos-standlone-mysql.env volumes: - /usr/nacos/logs/:/home/nacos/logs - /usr/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties ports: - "8848:8848" - "9555:9555" network_mode: host restart: on-failure
|
Docker Compose 指定环境变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| version: "3.5"
services: nacos: image: nacos:2.0.2 container_name: nacos-server privileged: false restart: always ports: - 8848:8848 environment: - MODE=standalone - TRACK=-javaagent:/opt/skywalking-agent.jar - JAVA_OPTS=-Xms512m -Xmx1024m -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC
|
参考资料