Docker Compose

2017-01-17  本文已影响267人  DongGuangqing

compose file 是一个YAML文件, 用来定义services, networks 和 volumes。 默认的文件路径是:./docker-compose.yml

services 用来定义一些该服务下容器公共的配置,类似于通过 docker run 传递命令行参数。
network 类似于 docker network create
volume 类似于 docker volume create.

可以在配置文件中使用环境变量: ${VARIABLE}

service 定义

container_name: 指定container 的名称
services:
  bls-wfe:
    container_name: bls-wfe
depends_on: 服务间依赖描述

docker-compose up will start services in dependency order.
docker-compose up SERVICE will automatically include SERVICE’s
dependencies.

version: '2'
services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres
build : Configuration options that are applied at build time.

用来指定build context 的路径

services:
  serviceA: 
    build: ./dir
  serviceB:
    build:
      context: ./dir
      dockerfile: Dockerfile-alternate
      args:
        buildno: 1 
env_file : 指定环境变量文件
services:
  serviceA: 
    env_file: .env
  serviceB:
    env_file:
      - ./common.env
      - ./apps/web.env
      - /opt/secrets.env
environment: 增加环境变量

Any boolean values; true, false, yes no, need to be enclosed in quotes to ensure they are not converted to True or False by the YML parser.

services:
  serviceA: 
    environment:
      # use dictionary
      RACK_ENV: development
      SHOW: 'true'
      SESSION_SECRET:
  serviceB:
    environment:
      # use array
      - RACK_ENV=development
      - SHOW=true
      - SESSION_SECRET
expose: 暴露端口

Expose ports without publishing them to the host machine - they’ll only be accessible to linked services. Only the internal port can be specified.

services:
  serviceA: 
    expose:
      - "3000"
      - "8000"
ports : 暴露host 端口
ports:
 - "3000"
 - "3000-3005" #  just the container port (a random host port will be chosen).
 - "8000:8000"
 - "9090-9091:8080-8081" #(HOST:CONTAINER)
 - "49100:22" 
 - "127.0.0.1:8001:8001"
 - "127.0.0.1:5000-5010:5000-5010"
extends: Extend another service
extends:
  file: common.yml
  service: webapp
image: Specify the image to start the container from.
# use  repository/tag
image: example-registry.com:4000/postgresql
# use  partial image ID
image: a4bc65fd
labels: 在 containers 中增加metadata信息
# use dictionary
labels:
  com.example.description: "Accounting webapp"
  com.example.department: "Finance"
  com.example.label-with-empty-value: ""

# use array
labels:
  - "com.example.description=Accounting webapp"
  - "com.example.department=Finance"
  - "com.example.label-with-empty-value"
links: Link to containers in another service.

Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.

Links also express dependency between services in the same way as depends_on, so they determine the order of service startup.

web:
  links:
   - db
   - db:database  #(SERVICE:ALIAS)
   - redis #  just the service name.
external_links

Link to containers started outside this docker-compose.yml or even outside of Compose, especially for containers that provide shared or common services.

external_links:
 - redis_1
 - project_db_1:mysql #  (CONTAINER:ALIAS)
 - project_db_1:postgresql 
net/network_mode :
network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"
extra_hosts: Add hostname mappings.
extra_hosts:
 - "somehost:162.242.195.82"
 - "otherhost:50.31.209.229"
volumes : specify a path on the host machine (HOST:CONTAINER), or an access mode (HOST:CONTAINER:ro).
volumes:
  # Just specify a path and let the Engine create a volume
  - /var/lib/mysql

  # Specify an absolute path mapping
  - /opt/data:/var/lib/mysql

  # Path on the host, relative to the Compose file
  - ./cache:/tmp/cache

  # User-relative path
  - ~/configs:/etc/configs/:ro

  # Named volume
  - datavolume:/var/lib/mysql

Compose file reference

上一篇下一篇

猜你喜欢

热点阅读