Docker Compose cheat sheet

Basic config example

1
# docker-compose.yml
2
version: '3'
3
4
services:
5
web:
6
build: .
7
# build from Dockerfile
8
context: ./Path
9
dockerfile: Dockerfile
10
ports:
11
- "5000:5000"
12
volumes:
13
- .:/code
14
redis:
15
image: redis
Copied!

Common commands

Config file reference

Building

1
web:
2
# build from Dockerfile
3
build: .
4
# build from custom Dockerfile
5
build:
6
context: ./dir
7
dockerfile: Dockerfile.dev
8
# build from image
9
image: ubuntu
10
image: ubuntu:14.04
11
image: tutum/influxdb
12
image: example-registry:4000/postgresql
13
image: a4bc65fd
Copied!

Ports

1
ports:
2
- "3000"
3
- "8000:80" # guest:host
4
# expose ports to linked services (not to host)
5
expose: ["3000"]
Copied!

Commands

1
# command to execute
2
command: bundle exec thin -p 3000
3
command: [bundle, exec, thin, -p, 3000]
4
5
# override the entrypoint
6
entrypoint: /app/start.sh
7
entrypoint: [php, -d, vendor/bin/phpunit]
Copied!

Environment variables

1
# environment vars
2
environment:
3
RACK_ENV: development
4
environment:
5
- RACK_ENV=development
6
7
# environment vars from file
8
env_file: .env
9
env_file: [.env, .development.env]
Copied!

Dependencies

1
# makes the `db` service available as the hostname `database`
2
# (implies depends_on)
3
links:
4
- db:database
5
- redis
6
7
# make sure `db` is alive before starting
8
depends_on:
9
- db
Copied!

Other options

1
# make this service extend another
2
extends:
3
file: common.yml # optional
4
service: webapp
5
volumes:
6
- /var/lib/mysql
7
- ./_data:/var/lib/mysql
Copied!

Advanced features

Labels

1
services:
2
web:
3
labels:
4
com.example.description: "Accounting web app"
Copied!

DNS servers

1
services:
2
web:
3
dns: 8.8.8.8
4
dns:
5
- 8.8.8.8
6
- 8.8.4.4
Copied!

Devices

1
services:
2
web:
3
devices:
4
- "/dev/ttyUSB0:/dev/ttyUSB0"
Copied!
1
services:
2
web:
3
external_links:
4
- redis_1
5
- project_db_1:mysql
Copied!

Hosts

1
services:
2
web:
3
extra_hosts:
4
- "somehost:192.168.1.100"
Copied!

Network

1
# creates a custom network called `frontend`
2
networks:
3
frontend:
Copied!

External network

1
# join a preexisting network
2
networks:
3
default:
4
external:
5
name: frontend
Copied!
.
.
.
----
.
Last modified 1yr ago