DEV Community

Cover image for Docker-compose cheat sheet
Matteo Barbero
Matteo Barbero

Posted on

Docker-compose cheat sheet

A cheat sheet to create you docker-compose.yml version: '3.8'

Build the image

web:
  # build from Dockerfile
  build: .
  # alternative to use a folder with more than one Dockerfile:
  build:
    context: ./dir # path to Dockerfile
    dockerfile: Dockerfile.dev #name of the file
    target: dev # specify the target in a multi-stage build
  # build from image
  image: ubuntu
  image: ubuntu:14.04
Enter fullscreen mode Exit fullscreen mode

Init

Run the service as PID 1. Set this option to true to enable this feature for the service.

services:
  web:
    image: alpine:latest
    init: true
Enter fullscreen mode Exit fullscreen mode

Expose port

ports:
  - "3000"
  - "8000:80"  # guest:host
# expose ports to linked services (not to host)
expose: ["3000"]
Enter fullscreen mode Exit fullscreen mode

Commands and Entrypoint

# command to execute
command: bundle exec thin -p 3000
command: [bundle, exec, thin, -p, 3000]

# override the entrypoint
entrypoint: /app/start.sh
entrypoint: [php, -d, vendor/bin/phpunit]
Enter fullscreen mode Exit fullscreen mode

Environment variables

# environment vars
environment:
  RACK_ENV: development
environment:
  - RACK_ENV=development

# environment vars from file
env_file: .env
env_file: [.env, .development.env]
Enter fullscreen mode Exit fullscreen mode

Health check

# declare service healthy when `test` command succeed
healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost"]
  interval: 1m30s
  timeout: 10s
  retries: 3
  start_period: 40s
Enter fullscreen mode Exit fullscreen mode

Dependencies

# make sure `db` is alive before starting
depends_on:
  - db


# make sure `db` is healthy before starting
# and db-init completed without failure
depends_on:
db:
  condition: service_healthy
db-init:
  condition: service_completed_successfully

Enter fullscreen mode Exit fullscreen mode

Volumes

volumes:
  - /var/lib/mysql # anonimous volume
  - ./host_path:/docker_container_path # bind mount
Enter fullscreen mode Exit fullscreen mode
volumes:
  - type: bind
    source: ./host_path
    target: /docker_container_path
  - type: volume
    # no source. Override the previous volumes for a specific path 
    target: /node_modules
Enter fullscreen mode Exit fullscreen mode

Restart

# automatically restart container
restart: unless-stopped
# always, on-failure, no (default)
Enter fullscreen mode Exit fullscreen mode

User

# specifying user
user: root
Enter fullscreen mode Exit fullscreen mode
# specifying both user and group with ids
user: 0:0
Enter fullscreen mode Exit fullscreen mode

Commands

# Starts existing containers.
docker-compose start

# Stops running containers.
docker-compose stop

# Pauses running containers.
docker-compose pause

# Unpauses paused.
docker-compose unpause

# Lists containers.
docker-compose ps

# Builds, (re)creates, starts, and attaches to containers for a service.
docker-compose up

# Stops and removes containers.
docker-compose down
Enter fullscreen mode Exit fullscreen mode

If you want to contribute leave a comment or fork the project repo:
https://github.com/maiobarbero/docker_compose_cheatsheet

Started from https://devhints.io/docker-compose

Top comments (4)

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

Good one!

Collapse
 
maiobarbero profile image
Matteo Barbero

Thanks for your appreciation.

Collapse
 
pablosalas81 profile image
pablosalas81

Muy buena info hermano, felicitaciones!!

Collapse
 
maiobarbero profile image
Matteo Barbero

Thanks😀