docker_practice/compose/compose_file.md

580 lines
12 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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