什么是docker-compose

compose是用来在docker中定义和运行复杂应用的小工具,简单的来说在配置文件中定义多个容器,然后通过命令就可以让定义多个容器都启动起来,而不需要一个手动启动起来。

https://github.com/docker/compose/releases/

###docker-compose 1.20.1

wget https://github.com/docker/compose/releases/download/1.20.1/docker-compose-Linux-x86_64

mv -f docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

一般步骤
1、定义Dockerfile,方便迁移到任何地方;

2、编写docker-compose.yml文件;

3、运行docker-compose up启动服务

默认是前台运行并打印日志到控制台。如果想后台运行,可以:

docker-compose up -d

服务后台后,可以使用下列命令查看状态:

docker-compose ps

停止服务:

docker-compose stop

重新启动服务:
docker-compose restart

#########################################

要想使用docker-compose就需要用到配置文件,

配置文件名可以为docker-compose.yml,docker-compose.yaml其中一个,在里面写入
docker-compose.yml参考
https://docs.docker.com/compose/compose-file

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

配置项详解
开头格式:
version: ‘3.6’ —-docker-compose支持的版本
services:
container_name 容器名字

image
指定镜像tag或者ID。示例:
image: redis
image: ubuntu:14.04
image: tutum/influxdb
image: example-registry.com:4000/postgresql
image: a4bc65fd

build
用来指定一个包含Dockerfile文件的路径。一般是当前目录.。Compose将会利用它自动构建这个镜像,然后使用这个镜像,Fig将build并生成一个随机命名的镜像。

build: ./dir

build:
context: ./dir
dockerfile: Dockerfile-alternate
args:
buildno: 1

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

command
覆盖容器启动后默认执行的命令。示例:
command: bundle exec thin -p 3000
command也支持数组形式:
command: [bundle, exec, thin, -p, 3000]

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

  • db
  • db:mysql
  • redis
    使用了别名将自动会在容器的/etc/hosts文件里创建相应记录:
    172.17.2.186 db
    172.17.2.186 mysql
    172.17.2.187 redis
    所以我们在容器里就可以直接使用别名作为服务的主机名。

ports
用于端口映射。同docker run -p。示例:
注意:端口映射与network_mode: host 不兼容

ports:

  • “3000”
  • “3000-3005”
  • “8000:8000”
  • “9090-9091:8080-8081”
  • “49100:22”
  • “127.0.0.1:8001:8001”
  • “127.0.0.1:5000-5010:5000-5010”
  • “6060:6060/udp”

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

  • “3000”
  • “8000”
    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
    environment
    添加环境变量。同docker run -e。可以是数组或者字典格式:
    environment:
    RACK_ENV: development
    SHOW: ‘true’
    SESSION_SECRET:

environment:

  • RACK_ENV=development
  • SHOW=true
  • SESSION_SECRET

depends_on
用于指定服务依赖,一般是mysql、redis等要在web服务之前启动。
指定了依赖,将会优先于服务创建并启动依赖。
links也可以指定依赖。
示例:
services:
web:
build: .
depends_on:

- db
- redis

redis:
image: redis
db:
image: mysql

external_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是需要继承的服务,例如web、database。

logging
Logging configuration for the service.
logging:
driver: syslog
options:
syslog-address: “tcp://192.168.0.42:123”

The default value is json-file.
driver: “json-file”
driver: “syslog”
driver: “none”


logging:
  driver: "json-file"
  options:
    max-size: "10M"
    max-file: "10"

network_mode
plus the special form service:[service name].
设置网络模式。同docker的–net参数。
network_mode: “bridge”
network_mode: “host”
network_mode: “none”
network_mode: “service:[service name]”
network_mode: “container:[container name/id]”

dns
自定义dns服务器。
dns: 8.8.8.8
dns:

  • 8.8.8.8
  • 9.9.9.9

healthcheck

healthcheck:
test: [“CMD”, “curl”, “-f”, “http://localhost“]
interval: 1m30s
timeout: 10s
retries: 3
start_period: 40s

test: [“CMD”, “curl”, “-f”, “http://localhost“]
test: [“CMD-SHELL”, “curl -f http://localhost || exit 1”]
test: curl -f https://localhost || exit 1

sysctls
Kernel parameters to set in the container
sysctls:
net.core.somaxconn: 1024
net.ipv4.tcp_syncookies: 0

sysctls:

  • net.core.somaxconn=1024
  • net.ipv4.tcp_syncookies=0

ulimits
Override the default ulimits for a container.
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000

devices
List of device mappings. Uses the same format as the –device docker client create option.
devices:

  • “/dev/ttyUSB0:/dev/ttyUSB0”

entrypoint
Override the default entrypoint.
entrypoint: /code/entrypoint.sh

The entrypoint can also be a list, in a manner similar to dockerfile:
entrypoint:

- php
- -d
- zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
- -d
- memory_limit=-1
- vendor/bin/phpunit

env_file
从文件中获取环境变量,可以为单独的文件路径或列表

env_file: .env

env_file:

  • ./common.env
  • ./apps/web.env
  • /opt/secrets.env

restart
no is the default restart policy, and it does not restart a container under any circumstance. When always is specified, the container always restarts. The on-failure policy restarts a container if the exit code indicates an on-failure error.
restart: “no”
restart: always
restart: on-failure
restart: unless-stopped

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

###############################

networks
Networks to join, referencing entries under the top-level networks key.
services:
some-service:
networks:

- some-network
- other-network

networks:
maiya-network:
driver: bridge
name: maiya-network

docker-compose常用命令
查看版本信息 docker-compose –version
构建或重新构建服务 docker-compose build(构建配置文件中的所有service),docker-compose build [service-name …],
service-name就是配置文件中指定的容器名
查看所有服务 docker-compose ps [service-name]
启动暂停杀死删除重启服务 docker-compose start/stop/kill/rm/restart [service-name …]
运行服务,相当于docker run,docker-compose up -d [service-name …]指定-d可以服务运行在后台
查看服务的日志信息 docker-compose logs [service-name]
pull服务镜像 docker-compose pull [service-name]
打印绑定的端口信息 docker-compose port [service-name]

##########################################################################
$ docker-compose -h
Define and run multi-container applications with Docker.

Usage:
docker-compose [-f=…] [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)
–x-networking (EXPERIMENTAL) Use new Docker networking functionality.
Requires Docker 1.9 or later.
–x-network-driver DRIVER (EXPERIMENTAL) Specify a network driver (default: “bridge”).
Requires Docker 1.9 or later.
–verbose Show more output
-v, –version Print version and exit

Commands:
build Build or rebuild services
help Get help on a command
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pulls 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
unpause Unpause services
up Create and start containers
migrate-to-labels Recreate containers to add labels
version Show the Docker-Compose version information