docker-compose
简介
通常我们使用docker容器安装服务,是一个个安装的,这在服务比较多且互相依赖使用的时候就比较麻烦了。这时候使用docker-compose的话,就可以通过一个docker-compose.yml脚本一键安装运行,docker-compose是一个自动化编排容器。
Linux环境安装
如果你的Linux环境是centos
就使用yum -y install docker-compose
,或者环境是ubuntu
就使用apt-get install docker-compose
安装成功后使用docker-compose version
命令查看版本,如果如下显示出来就安装成功了。
1 2 3 4 5
| root@iZwz9b7fouo96fsoenxil7Z:~/example01# docker-compose version docker-compose version 1.8.0, build unknown docker-py version: 1.9.0 CPython version: 2.7.12 OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
|
配置文件构建参数
下面是官方的docker-compose.yml配置文件的例子,Compose文件是一个定义服务services
、网络networks
、卷volumes
的YAML文件,可以使用yml和yaml作为文件扩展名。compose配置文件中比较关键的就是services
和networks
这两个部分了。
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| version: "3" services:
redis: image: redis:alpine ports: - "6379" networks: - frontend deploy: replicas: 2 update_config: parallelism: 2 delay: 10s restart_policy: condition: on-failure
db: image: postgres:9.4 volumes: - db-data:/var/lib/postgresql/data networks: - backend deploy: placement: constraints: [node.role == manager]
vote: image: dockersamples/examplevotingapp_vote:before ports: - 5000:80 networks: - frontend depends_on: - redis deploy: replicas: 2 update_config: parallelism: 2 restart_policy: condition: on-failure
result: image: dockersamples/examplevotingapp_result:before ports: - 5001:80 networks: - backend depends_on: - db deploy: replicas: 1 update_config: parallelism: 2 delay: 10s restart_policy: condition: on-failure
worker: image: dockersamples/examplevotingapp_worker networks: - frontend - backend deploy: mode: replicated replicas: 1 labels: [APP=VOTING] restart_policy: condition: on-failure delay: 10s max_attempts: 3 window: 120s placement: constraints: [node.role == manager]
visualizer: image: dockersamples/visualizer:stable ports: - "8080:8080" stop_grace_period: 1m30s volumes: - "/var/run/docker.sock:/var/run/docker.sock" deploy: placement: constraints: [node.role == manager]
networks: frontend: backend:
volumes: db-data:
|
images
images用来指定服务的镜像名称或者镜像ID,如果镜像在本地不存在,compose就会尝试去拉取这个镜像。
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
| services: web: image:redis build: context: ../ dockerfile: path/Dockerfile command: [bundle, exec, thin, -p, 3000] container_name: app depends_on: postgres environment: - RACK_ENV=development - SHOW=true - SESSION_SECRE port: - "3000:3000" volumes: - /opt/data:/var/lib/mysql links: - elasticsearch:es networks: - some-network - other-network
|
docker-compose常用命令
- docker-compose up -d # 在后台启动服务
- docker-compose ps # 查看启动的服务
- docker-compose stop #停止服务
参考资料
docker-compose.yml - 简书 (jianshu.com)
docker-compose.yml 配置文件编写详解_独化蓝翅鸟,越岭万昆仑-CSDN博客_docker-compose.yml 配置文件编写详解