yml文件命令

version

指定 docker-compose.yml 文件的写法格式

  1. version: 3

services

多个容器集合

  1. services:

image

指定服务所使用的镜像

  1. image: postgres

environment

环境变量配置,可以用数组或字典两种方式

  1. environment:
  2. RACK_ENV: development
  3. SHOW: 'ture'
  4. -------------------------
  5. environment:
  6. - RACK_ENV=development
  7. - SHOW=ture

ports

对外暴露的端口定义,和 expose 对应

  1. ports: # 暴露端口信息 - "宿主机端口:容器暴露端口"
  2. - "8763:8763"
  3. - "8763:8763"

volumes

卷挂载路径

  1. volumes:
  2. - /config:/ect/config

expose

暴露端口,只将端口暴露给连接的服务,而不暴露给主机

  1. expose:
  2. - "3000"
  3. - "8000"

command

覆盖容器启动后默认执行的命令

  1. command: bundle exec thin -p 3000
  2. ----------------------------------
  3. command: [bundle,exec,thin,-p,3000]

示例

  1. version: '3'
  2. services:
  3. server:
  4. image: postgrest/postgrest
  5. ports:
  6. - "3000:3000"
  7. links:
  8. - db:db
  9. environment:
  10. PGRST_DB_URI: postgres://qinyi:123456@db:5432/test
  11. PGRST_DB_SCHEMA: api
  12. PGRST_DB_ANON_ROLE: qinyi #In production this role should not be the same as the one used for the connection
  13. PGRST_SERVER_PROXY_URI: "http://127.0.0.1:3000"
  14. PGRST_JWT_SECRET: "oW3Fxhc07bE4vnsFzsMsnnsnb7FxSuMz"
  15. depends_on:
  16. - db
  17. db:
  18. image: postgres
  19. ports:
  20. - "5432:5432"
  21. environment:
  22. POSTGRES_DB: test
  23. POSTGRES_USER: root
  24. POSTGRES_PASSWORD: 123456
  25. # Uncomment this if you want to persist the data.
  26. volumes:
  27. - "~/postgrest/pgdata:/var/lib/postgresql/data"

常用命名

  1. //创建docker-compose.yml文件
  2. //该文件目录下运行,-d表示后台运行
  3. docker-compose up -d
  4. //停止某个服务
  5. docker-compose stop [service_name]
  6. //某个服务,启动并重新加载docker-compose.yml文件配置
  7. docker-compose up -d --build [service_name]