docker-compose.yml配置详解

博客分类: software 阅读次数: comments

docker-compose.yml配置详解

配置详解

每个docker-compose.yml必须定义 image 或者 build 中的一个,其它的是可选的。

image

指定镜像tag或者ID。示例:

image: redis
image: ubuntu:14.04
image: tutum/influxdb
image: example-registry.com:4000/postgresql
image: a4bc65fd

注意,在 version 1 里同时使用 imagebuild 是不允许的,version 2 则可以,如果同时指定了两者,会将 build 出来的镜像打上名为 image 标签。

build

用来指定一个包含 Dockerfile 文件的路径。一般是当前目录 . 。Fig将 build 并生成一个随机命名的镜像。

注意,在 version 1bulid 仅支持值为字符串。version 2 里支持对象格式。

build: php72/

php-fpm72:
    build:
        context: php72/
        dockerfile: Dockerfile-test
        args:
            LNMP_PHP72_VERSION: $LNMP_PHP72_VERSION

context 为路径, dockerfile 为需要替换默认 docker-compose 的文件名, args 为构建(build)过程中的环境变量,用于替换Dockerfile里定义的 ARG 参数,容器中不可用。示例: Dockerfile:

ARG LNMP_PHP72_VERSION

FROM php:$LNMP_PHP72_VERSION

docker-compose.yml:

php-fpm72:
    build:
        context: php72/
        dockerfile: Dockerfile-test
        args:
            LNMP_PHP72_VERSION: $LNMP_PHP72_VERSION

command

用来覆盖缺省命令。示例:

command: bundle exec thin -p 3000

command 也支持数组形式:

command: [bundle, exec, thin, -p, 3000]

用于链接另一容器服务,如需要使用到另一容器的mysql服务。可以给出服务名和别名;也可以仅给出服务名,这样别名将和服务名相同。同 docker run --link 。示例:

links:
 - php-fpm72
 - mysql-db
 - redis-db

使用了别名将自动会在容器的 /etc/hosts 文件里创建相应记录:

172.17.2.186  php-fpm72
172.17.2.186  mysql-db
172.17.2.187  redis-db

所以我们在容器里就可以直接使用别名作为服务的主机名。

ports

用于暴露端口。同 docker run -p 。示例:

ports:
    - "9000"
    - "80:80"
    - "3307:3306"
    - "127.0.0.1:6379:6379"

expose

expose提供container之间的端口访问,不会暴露给主机使用。同 docker run --expose

expose:
    - "9000"
    - "3306"

volumes

挂载数据卷。同 docker run -v 。示例:

volumes:
    - /var/lib/mysql
    - cache/:/tmp/cache
    - ~/configs:/etc/configs/:ro

volumes_from

挂载数据卷容器,挂载是容器。同 docker run --volumes-from 。示例:

volumes_from:
    - service_name
    - service_name:ro
    - container:container_name
    - container:container_name:rw

container:container_name 格式仅支持 version 2

environment

添加环境变量。同 docker run -e 。可以是数组或者字典格式:

environment:
    MYSQL_ROOT_PASSWORD: 123456
    MYSQL_DATABASE: db_test
    MYSQL_USER: test
    MYSQL_PASSWORD: test

environment:
    - MYSQL_ROOT_PASSWORD=123456
    - MYSQL_DATABASE=db_test
    - MYSQL_USER=test
    - MYSQL_PASSWORD=test

depends_on

用于指定服务依赖,一般是php-fpm等。
指定了依赖,将会优先于服务创建并启动依赖。
示例:

depends_on:
    - php-fpm72

links 也可以指定依赖

链接搭配 docker-compose.yml 文件或者 Compose 之外定义的服务,通常是提供共享或公共服务。格式与 links 相似:

external_links:
    - redis_1
    - project_db_1:mysql
    - project_db_1:postgresql

注意, external_links 链接的服务与当前服务必须是同一个网络环境。

extra_hosts

添加主机名映射。

extra_hosts:
    - "somehost:162.242.195.82"
    - "otherhost:50.31.209.229"

将会在/etc/hosts创建记录:

162.242.195.82  somehost
50.31.209.229   otherhost

extends

继承自当前yml文件或者其它文件中定义的服务,可以选择性的覆盖原有配置。

extends:
    file: common.yml
    service: webapp

service 必须有, file 可选。 service 是需要继承的服务,例如 webdatabase

net

设置网络模式。同docker的 --net 参数。

net: "bridge"
net: "none"
net: "container:[name or id]"
net: "host"

dns

自定义dns服务器。

dns: 8.8.8.8
dns:
    - 8.8.8.8
    - 9.9.9.9

其他命令

cpu_shares, cpu_quota, cpuset, domainname, hostname, ipc, mac_address, mem_limit, memswap_limit, privileged, read_only, restart, shm_size, stdin_open, tty, user, working_dir 这些命令都是单个值,含义请参考docker run

cpu_shares: 73
cpu_quota: 50000
cpuset: 0,1

user: postgresql
working_dir: /code

domainname: foo.com
hostname: foo
ipc: host
mac_address: 02:42:ac:11:65:43

mem_limit: 1000000000
mem_limit: 128M
memswap_limit: 2000000000
privileged: true

restart: always

read_only: true
shm_size: 64M
stdin_open: true
tty: true

命令行参考

~ » docker-compose
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [--] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file
                              (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)
  -c, --context NAME          Specify a context name
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the
                              name specified in the client certificate
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert keys
                              in v3 files to their non-Swarm equivalent (DEPRECATED)
  --env-file PATH             Specify an alternate environment file

Commands:
  build              Build or rebuild services
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show version information and quit

相关链接

原文链接:https://www.cnblogs.com/52fhy/p/5991344.html