docker_practice/compose/compose_file.md

580 lines
12 KiB
Go
Raw Normal View History

# Compose
2015-05-08 20:10:01 +08:00
2017-11-26 09:54:04 +08:00
使 `Compose` `docker run`
2015-05-08 20:10:01 +08:00
2016-12-20 15:35:52 +08:00
`docker-compose.yml` YAML
2015-05-08 20:10:01 +08:00
2016-12-20 15:35:52 +08:00
```yaml
2017-11-23 15:48:52 +08:00
version: "3"
2015-05-08 20:10:01 +08:00
2016-12-20 15:35:52 +08:00
services:
webapp:
image: examples/web
ports:
- "80:80"
volumes:
- "/data"
2015-05-08 20:10:01 +08:00
```
2016-12-20 15:35:52 +08:00
`image` `build` Dockerfile
使 `build` `Dockerfile` (`CMD`, `EXPOSE`, `VOLUME`, `ENV` ) `docker-compose.yml`
2016-12-20 15:35:52 +08:00
## `build`
2015-05-08 20:10:01 +08:00
2016-12-20 15:35:52 +08:00
`Dockerfile` docker-compose.yml `Compose` 使
2015-05-08 20:10:01 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2017-12-16 15:08:02 +08:00
version: '3'
services:
webapp:
build: ./dir
```
使 `context` `Dockerfile`
使 `dockerfile` `Dockerfile`
使 `arg`
```yaml
version: '3'
services:
webapp:
build:
context: ./dir
dockerfile: Dockerfile-alternate
args:
buildno: 1
```
使 `cache_from`
```yaml
build:
context: .
cache_from:
- alpine:latest
- corp/web_app:3.14
2015-05-08 20:10:01 +08:00
```
## `cap_add, cap_drop`
2017-11-23 21:25:55 +08:00
2016-12-20 15:35:52 +08:00
capacity
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
cap_add:
- ALL
```
NET_ADMIN
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
cap_drop:
- NET_ADMIN
```
## `command`
2015-05-08 20:10:01 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
command: echo "hello world"
2015-05-08 20:10:01 +08:00
```
## `configs`
2017-11-23 21:25:55 +08:00
`Swarm mode` [`Swarm mode`](../swarm_mode/)
## `cgroup_parent`
2017-11-23 21:25:55 +08:00
2017-11-23 15:48:52 +08:00
`cgroup`
2015-05-08 20:10:01 +08:00
2016-12-20 15:35:52 +08:00
cgroup `cgroups_1`
2015-05-08 20:10:01 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
cgroup_parent: cgroups_1
2015-05-08 20:10:01 +08:00
```
## `container_name`
2017-11-23 21:25:55 +08:00
2016-12-20 15:35:52 +08:00
使 `项目名称_服务名称_序号`
2015-05-08 20:10:01 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
container_name: docker-web-container
2015-05-08 20:10:01 +08:00
```
2017-12-16 15:08:02 +08:00
>: scale Docker
2015-05-08 20:10:01 +08:00
## `deploy`
2017-11-23 21:25:55 +08:00
`Swarm mode` [`Swarm mode`](../swarm_mode/)
## `devices`
2017-11-23 21:25:55 +08:00
2016-12-20 15:35:52 +08:00
2015-05-08 20:10:01 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
devices:
- "/dev/ttyUSB1:/dev/ttyUSB0"
2015-05-08 20:10:01 +08:00
```
## `depends_on`
2017-11-23 15:48:52 +08:00
2017-11-23 21:25:55 +08:00
`redis` `db` `web`
```yaml
version: '3'
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
```
2017-12-16 15:08:02 +08:00
>`web` `redis` `db`
## `dns`
2015-05-08 20:10:01 +08:00
2017-11-23 15:48:52 +08:00
`DNS`
2015-05-08 20:10:01 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
dns: 8.8.8.8
2017-11-23 15:48:52 +08:00
2016-12-20 15:35:52 +08:00
dns:
- 8.8.8.8
2017-11-23 15:48:52 +08:00
- 114.114.114.114
2016-12-20 15:35:52 +08:00
```
2015-05-08 20:10:01 +08:00
## `dns_search`
2015-05-08 20:10:01 +08:00
2017-11-23 15:48:52 +08:00
`DNS`
2016-12-20 15:35:52 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
dns_search: example.com
2017-11-23 15:48:52 +08:00
2016-12-20 15:35:52 +08:00
dns_search:
- domain1.example.com
- domain2.example.com
2015-05-08 20:10:01 +08:00
```
## `tmpfs`
2017-11-23 21:25:55 +08:00
tmpfs
```yaml
tmpfs: /run
tmpfs:
- /run
- /tmp
```
## `env_file`
2017-11-23 21:25:55 +08:00
2016-12-20 15:35:52 +08:00
2015-05-08 20:10:01 +08:00
2016-12-20 15:35:52 +08:00
`docker-compose -f FILE` Compose `env_file`
2015-05-08 20:10:01 +08:00
2016-12-20 15:35:52 +08:00
`environment`
2015-05-08 20:10:01 +08:00
2017-11-22 11:13:23 +08:00
```bash
2016-12-20 15:35:52 +08:00
env_file: .env
2015-05-08 20:10:01 +08:00
2016-12-20 15:35:52 +08:00
env_file:
- ./common.env
- ./apps/web.env
- /opt/secrets.env
2015-05-08 20:10:01 +08:00
```
2016-12-20 15:35:52 +08:00
`#`
2015-05-08 20:10:01 +08:00
2017-11-22 11:13:23 +08:00
```bash
2016-12-20 15:35:52 +08:00
# common.env: Set development environment
PROG_ENV=development
2015-05-08 20:10:01 +08:00
```
## `environment`
2015-05-08 20:10:01 +08:00
使
2016-12-20 15:35:52 +08:00
Compose
2015-05-08 20:10:01 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2015-05-08 20:10:01 +08:00
environment:
RACK_ENV: development
SESSION_SECRET:
environment:
- RACK_ENV=development
- SESSION_SECRET
```
2019-01-06 10:15:13 +08:00
`true|falseyes|no` [](https://yaml.org/type/bool.html) 含义的词汇,最好放到引号里,避免 YAML 自动解析某些内容为对应的布尔语义。这些特定词汇,包括
2015-05-08 20:10:01 +08:00
2017-11-22 11:13:23 +08:00
```bash
2017-11-23 15:48:52 +08:00
y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF
2016-12-20 15:35:52 +08:00
```
2015-05-08 20:10:01 +08:00
## `expose`
2015-05-08 20:10:01 +08:00
2016-12-20 15:35:52 +08:00
宿访
2015-05-08 20:10:01 +08:00
2016-12-20 15:35:52 +08:00
2015-05-08 20:10:01 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
expose:
- "3000"
- "8000"
2015-05-08 20:10:01 +08:00
```
## `external_links`
2017-11-23 21:25:55 +08:00
2017-12-16 15:08:02 +08:00
>使
`docker-compose.yml` `Compose`
2016-12-20 15:35:52 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
```
## `extra_hosts`
2017-11-23 21:25:55 +08:00
2016-12-20 15:35:52 +08:00
Docker `--add-host` host
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
extra_hosts:
- "googledns:8.8.8.8"
- "dockerhub:52.1.157.61"
```
`/etc/hosts`
2017-11-23 15:48:52 +08:00
2017-11-22 11:13:23 +08:00
```bash
2016-12-20 15:35:52 +08:00
8.8.8.8 googledns
52.1.157.61 dockerhub
```
## `healthcheck`
2017-11-23 15:48:52 +08:00
2017-11-23 21:25:55 +08:00
```yaml
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 1m30s
timeout: 10s
retries: 3
```
## `image`
2016-12-20 15:35:52 +08:00
2018-03-22 12:31:35 +08:00
ID`Compose`
2016-12-20 15:35:52 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
image: ubuntu
image: orchardup/postgresql
image: a4bc65fd
```
## `labels`
2017-11-23 21:25:55 +08:00
2016-12-20 15:35:52 +08:00
Docker metadata
2017-11-23 21:25:55 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
labels:
com.startupteam.description: "webapp for a startup team"
com.startupteam.department: "devops department"
com.startupteam.release: "rc3 for v1.0"
```
## `links`
2016-12-20 15:35:52 +08:00
2017-12-16 15:08:02 +08:00
>使
2016-12-20 15:35:52 +08:00
## `logging`
2017-11-23 21:25:55 +08:00
```yaml
logging:
driver: syslog
options:
syslog-address: "tcp://192.168.0.42:123"
```
2016-12-20 15:35:52 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2017-11-23 21:25:55 +08:00
driver: "json-file"
driver: "syslog"
driver: "none"
2016-12-20 15:35:52 +08:00
```
2017-11-23 21:25:55 +08:00
`options`
2016-12-20 15:35:52 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2017-11-23 21:25:55 +08:00
options:
max-size: "200k"
max-file: "10"
2016-12-20 15:35:52 +08:00
```
2015-05-08 20:10:01 +08:00
## `network_mode`
2015-05-08 20:10:01 +08:00
2017-12-16 15:08:02 +08:00
使 `docker run` `--network`
2015-05-08 20:10:01 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2017-11-23 21:25:55 +08:00
network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"
```
## `networks`
2017-11-23 21:25:55 +08:00
```yaml
version: "3"
services:
some-service:
networks:
- some-network
- other-network
networks:
some-network:
other-network:
2015-05-08 20:10:01 +08:00
```
## `pid`
2017-11-23 21:25:55 +08:00
2016-12-20 15:35:52 +08:00
宿 ID 访
2015-05-10 14:56:05 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2015-05-10 14:56:05 +08:00
pid: "host"
```
## `ports`
2015-05-08 20:10:01 +08:00
2016-12-20 15:35:52 +08:00
2017-12-16 15:08:02 +08:00
使宿 `(HOST:CONTAINER)` 宿
2016-12-20 15:35:52 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
ports:
- "3000"
- "8000:8000"
- "49100:22"
- "127.0.0.1:8001:8001"
2015-05-08 20:10:01 +08:00
```
2016-12-20 15:35:52 +08:00
*使 `HOST:CONTAINER` 使 60 `YAML` `xx:yy` 60 *
2015-05-10 14:56:05 +08:00
## `secrets`
2017-11-23 21:25:55 +08:00
2017-12-16 15:08:02 +08:00
`mysql`
```yaml
version: "3.1"
2017-12-16 15:08:02 +08:00
services:
mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
secrets:
- db_root_password
- my_other_secret
secrets:
my_secret:
file: ./my_secret.txt
my_other_secret:
external: true
```
2017-11-23 21:25:55 +08:00
## `security_opt`
2016-12-20 15:35:52 +08:00
2017-11-23 21:25:55 +08:00
label
2016-12-20 15:35:52 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
security_opt:
- label:user:USER
- label:role:ROLE
2015-05-10 14:56:05 +08:00
```
## `stop_signal`
2017-11-23 21:25:55 +08:00
使 SIGTERM
```yaml
stop_signal: SIGUSR1
```
## `sysctls`
2017-11-23 21:25:55 +08:00
```yaml
sysctls:
net.core.somaxconn: 1024
net.ipv4.tcp_syncookies: 0
sysctls:
- net.core.somaxconn=1024
- net.ipv4.tcp_syncookies=0
```
## `ulimits`
2017-11-23 21:25:55 +08:00
2016-12-20 15:35:52 +08:00
ulimits
2015-05-10 14:56:05 +08:00
2016-12-20 15:35:52 +08:00
65535 20000 40000 root
2015-05-10 14:56:05 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
```
## `volumes`
2016-12-20 15:35:52 +08:00
宿(`HOST:CONTAINER`)(`VOLUME:CONTAINER`)访 `HOST:CONTAINER:ro`
2016-12-20 15:35:52 +08:00
2017-11-23 15:48:52 +08:00
2016-12-20 15:35:52 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
volumes:
- /var/lib/mysql
- cache/:/tmp/cache
- ~/configs:/etc/configs/:ro
2015-05-10 14:56:05 +08:00
```
```yaml
version: "3"
services:
my_src:
image: mysql:8.0
volumes:
- mysql_data:/var/lib/mysql
volumes:
mysql_data:
```
##
2016-12-20 15:35:52 +08:00
2017-11-23 15:48:52 +08:00
`domainname, entrypoint, hostname, ipc, mac_address, privileged, read_only, shm_size, restart, stdin_open, tty, user, working_dir` `docker run`
2015-05-10 14:56:05 +08:00
2017-11-23 15:48:52 +08:00
2017-11-23 21:25:55 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2015-05-08 20:10:01 +08:00
entrypoint: /code/entrypoint.sh
2016-12-20 15:35:52 +08:00
```
2017-11-23 21:25:55 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
user: nginx
```
2017-11-23 21:25:55 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
working_dir: /code
```
mac
2017-11-23 21:25:55 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
domainname: your_website.com
hostname: test
mac_address: 08-00-27-00-0C-0A
```
2015-05-08 20:10:01 +08:00
2016-12-20 15:35:52 +08:00
2017-11-23 21:25:55 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2015-05-08 20:10:01 +08:00
privileged: true
2016-12-20 15:35:52 +08:00
```
2015-05-10 14:56:05 +08:00
2016-12-20 15:35:52 +08:00
退 `always` `unless-stopped`
2017-11-23 21:25:55 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2015-05-10 14:56:05 +08:00
restart: always
2016-12-20 15:35:52 +08:00
```
root
2017-11-23 21:25:55 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2016-12-20 15:35:52 +08:00
read_only: true
```
2015-05-10 14:56:05 +08:00
2016-12-20 15:35:52 +08:00
2017-11-23 21:25:55 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2015-05-10 14:56:05 +08:00
stdin_open: true
2016-12-20 15:35:52 +08:00
```
2017-12-16 15:08:02 +08:00
2017-11-23 21:25:55 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2015-05-10 14:56:05 +08:00
tty: true
2015-05-08 20:10:01 +08:00
```
2016-12-20 15:35:52 +08:00
##
2017-11-23 15:48:52 +08:00
2017-12-16 15:08:02 +08:00
Compose `.env`
2016-12-20 15:35:52 +08:00
2017-12-16 15:08:02 +08:00
Compose `${MONGO_VERSION}`
2016-12-20 15:35:52 +08:00
2017-11-23 15:48:52 +08:00
```yaml
2017-12-16 15:08:02 +08:00
version: "3"
services:
2016-12-20 15:35:52 +08:00
db:
image: "mongo:${MONGO_VERSION}"
```
2017-11-29 17:19:25 +08:00
`MONGO_VERSION=3.2 docker-compose up` `mongo:3.2` `MONGO_VERSION=2.8 docker-compose up` `mongo:2.8`
2017-12-16 15:08:02 +08:00
`.env` `docker-compose`
`.env`
```bash
# #
MONGO_VERSION=3.6
```
`docker-compose up` `mongo:3.6`
##
* [](https://docs.docker.com/compose/compose-file/)
* [awesome-compose](https://github.com/docker/awesome-compose)