docker_practice/compose/compose_file.md

580 lines
12 KiB
Go
Raw Permalink Normal View History

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