cheatsheets/docker-compose.md

304 lines
4.4 KiB
Markdown
Raw Permalink Normal View History

2016-06-29 09:50:06 +00:00
---
title: docker-compose
category: Devops
2017-09-20 14:00:15 +00:00
prism_languages: [yaml]
weight: -1
updated: 2024-04-03
2016-06-29 09:50:06 +00:00
---
2017-09-20 14:00:15 +00:00
### Basic example
2016-06-29 09:50:06 +00:00
```yaml
# docker-compose.yml
version: '2'
services:
web:
2022-05-20 02:43:57 +00:00
build:
2018-03-17 05:28:27 +00:00
# build from Dockerfile
2022-05-20 02:43:57 +00:00
context: ./Path
dockerfile: Dockerfile
2016-06-29 09:50:06 +00:00
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: redis
```
### Version 1 to 2
Docker compose is now integrated into the official Docker installation. The functionality only improved over that change, and the simple syntax change is : V1 : `docker-compose ARG` to V2 `docker compose ARG`
More on that here : [Docker Compose](https://docs.docker.com/compose/) [Migrate to V2](https://docs.docker.com/compose/migrate/)
2017-09-20 14:00:15 +00:00
### Commands
2016-07-06 08:43:31 +00:00
```sh
docker compose version
docker compose config
```
2016-06-29 09:50:06 +00:00
```sh
docker compose start
docker compose stop
docker compose restart
docker compose run
2017-09-20 14:00:15 +00:00
```
2016-07-06 08:43:31 +00:00
2017-09-20 14:00:15 +00:00
```sh
docker compose create
docker compose attach
docker compose pause
docker compose unpause
2017-09-20 14:00:15 +00:00
```
2016-07-06 08:43:31 +00:00
2017-09-20 14:00:15 +00:00
```sh
docker compose wait
docker compose up
docker compose down
```
```sh
docker compose ps
docker compose top
docker compose events
docker compose logs
```
```sh
docker compose images
docker compose build
docker compose push
docker compose cp
docker compose exec
2016-07-06 08:43:31 +00:00
```
## Reference
2018-06-26 14:45:48 +00:00
{: .-three-column}
### Building
2016-07-06 08:43:31 +00:00
```yaml
web:
# build from Dockerfile
build: .
args: # Add build arguments
APP_HOME: app
2018-06-26 14:45:48 +00:00
```
```yaml
# build from custom Dockerfile
build:
context: ./dir
dockerfile: Dockerfile.dev
```
2016-07-06 08:43:31 +00:00
2018-06-26 14:45:48 +00:00
```yaml
2016-07-06 08:43:31 +00:00
# build from image
image: ubuntu
image: ubuntu:14.04
image: tutum/influxdb
image: example-registry:4000/postgresql
image: a4bc65fd
2018-06-26 14:45:48 +00:00
```
### Ports
2016-07-06 08:43:31 +00:00
2018-06-26 14:45:48 +00:00
```yaml
2016-07-06 08:43:31 +00:00
ports:
- "3000"
2019-11-18 15:34:02 +00:00
- "8000:80" # host:container
2018-06-26 14:45:48 +00:00
```
```yaml
# expose ports to linked services (not to host)
expose: ["3000"]
```
### Commands
2016-07-06 08:43:31 +00:00
2018-06-26 14:45:48 +00:00
```yaml
2016-07-06 08:43:31 +00:00
# command to execute
command: bundle exec thin -p 3000
command: [bundle, exec, thin, -p, 3000]
2018-06-26 14:45:48 +00:00
```
2016-07-06 08:43:31 +00:00
2018-06-26 14:45:48 +00:00
```yaml
2016-07-06 08:43:31 +00:00
# override the entrypoint
entrypoint: /app/start.sh
entrypoint: [php, -d, vendor/bin/phpunit]
2018-06-26 14:45:48 +00:00
```
### Environment variables
2016-07-06 08:43:31 +00:00
2018-06-26 14:45:48 +00:00
```yaml
2016-07-06 08:43:31 +00:00
# environment vars
environment:
RACK_ENV: development
environment:
- RACK_ENV=development
2018-06-26 14:45:48 +00:00
```
2016-07-06 08:43:31 +00:00
2018-06-26 14:45:48 +00:00
```yaml
2016-07-06 08:43:31 +00:00
# environment vars from file
env_file: .env
env_file: [.env, .development.env]
2018-06-26 14:45:48 +00:00
```
2016-07-06 08:43:31 +00:00
2018-06-26 14:45:48 +00:00
### Dependencies
2016-07-06 08:43:31 +00:00
2018-06-26 14:45:48 +00:00
```yaml
2016-07-06 08:43:31 +00:00
# makes the `db` service available as the hostname `database`
# (implies depends_on)
links:
- db:database
- redis
2018-06-26 14:45:48 +00:00
```
2016-07-06 08:43:31 +00:00
2018-06-26 14:45:48 +00:00
```yaml
2016-07-06 08:43:31 +00:00
# make sure `db` is alive before starting
depends_on:
- db
2018-06-26 14:45:48 +00:00
```
```yaml
# make sure `db` is healty before starting
# and db-init completed without failure
depends_on:
db:
condition: service_healthy
db-init:
condition: service_completed_successfully
```
2018-06-26 14:45:48 +00:00
### Other options
2016-07-06 08:43:31 +00:00
2018-06-26 14:45:48 +00:00
```yaml
# make this service extend another
extends:
file: common.yml # optional
service: webapp
```
```yaml
2016-07-06 08:43:31 +00:00
volumes:
- /var/lib/mysql
- ./_data:/var/lib/mysql
```
```yaml
# automatically restart container
restart: unless-stopped
# always, on-failure, no (default)
```
2017-09-20 14:00:15 +00:00
## Advanced features
{: .-three-column}
### Labels
2016-07-06 08:43:31 +00:00
```yaml
2017-09-20 14:00:15 +00:00
services:
web:
labels:
com.example.description: "Accounting web app"
```
### DNS servers
```yaml
services:
web:
dns: 8.8.8.8
dns:
- 8.8.8.8
- 8.8.4.4
```
2016-07-06 08:43:31 +00:00
2017-09-20 14:00:15 +00:00
### Devices
2016-07-06 08:43:31 +00:00
2017-09-20 14:00:15 +00:00
```yaml
services:
web:
devices:
- "/dev/ttyUSB0:/dev/ttyUSB0"
```
2016-07-06 08:43:31 +00:00
2017-09-20 14:00:15 +00:00
### External links
2016-07-06 08:43:31 +00:00
2017-09-20 14:00:15 +00:00
```yaml
services:
web:
external_links:
- redis_1
- project_db_1:mysql
```
2022-11-01 02:26:22 +00:00
### Healthcheck
```yaml
# declare service healthy when `test` command succeed
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 1m30s
timeout: 10s
retries: 3
start_period: 40s
```
2017-09-20 14:00:15 +00:00
### Hosts
```yaml
services:
web:
extra_hosts:
- "somehost:192.168.1.100"
2016-06-29 09:50:06 +00:00
```
2018-11-16 19:54:03 +00:00
### Network
```yaml
# creates a custom network called `frontend`
networks:
frontend:
```
### External network
```yaml
# join a pre-existing network
networks:
default:
external:
name: frontend
```
### Volume
```yaml
2022-05-20 02:43:57 +00:00
# mount host paths or named volumes, specified as sub-options to a service
db:
image: postgres:latest
volumes:
- "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
- "dbdata:/var/lib/postgresql/data"
volumes:
dbdata:
```
2022-05-20 02:43:57 +00:00
### User
```yaml
# specifying user
user: root
```
```yaml
# specifying both user and group with ids
user: 0:0
```