Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save samqi/f37127e57eda56183bfd96c24333fecb to your computer and use it in GitHub Desktop.
Save samqi/f37127e57eda56183bfd96c24333fecb to your computer and use it in GitHub Desktop.
Docker Compose Cheatsheet
title subtitle author date source
Docker Compose Cheatsheet
Quick reference for Docker Compose commands and config files
Jon LaBelle
April 7, 2019

Basic config example

# docker-compose.yml
version: '3'

services:
  web:
    build: .
    # build from Dockerfile
    context: ./Path
    dockerfile: Dockerfile
    ports:
     - "5000:5000"
    volumes:
     - .:/code
  redis:
    image: redis

Common commands

# Starts existing containers for a service.
docker-compose start

# Stops running containers without removing them.
docker-compose stop

# Pauses running containers of a service.
docker-compose pause

# Unpauses paused containers of a service.
docker-compose unpause

# Lists containers.
docker-compose ps

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

# Stops containers and removes containers, networks, volumes, and images created by up.
docker-compose down

Config file reference

Building

web:
  # build from Dockerfile
  build: .
  # build from custom Dockerfile
  build:
    context: ./dir
    dockerfile: Dockerfile.dev
  # build from image
  image: ubuntu
  image: ubuntu:14.04
  image: tutum/influxdb
  image: example-registry:4000/postgresql
  image: a4bc65fd

Ports

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

Commands

# 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]

Environment variables

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

# environment vars from file
env_file: .env
env_file: [.env, .development.env]

Dependencies

# makes the `db` service available as the hostname `database`
# (implies depends_on)
links:
  - db:database
  - redis

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

Other options

# make this service extend another
extends:
  file: common.yml  # optional
  service: webapp
volumes:
  - /var/lib/mysql
  - ./_data:/var/lib/mysql

Advanced features

Labels

services:
  web:
    labels:
      com.example.description: "Accounting web app"

DNS servers

services:
  web:
    dns: 8.8.8.8
    dns:
      - 8.8.8.8
      - 8.8.4.4

Devices

services:
  web:
    devices:
      - "/dev/ttyUSB0:/dev/ttyUSB0"

External links

services:
  web:
    external_links:
      - redis_1
      - project_db_1:mysql

Hosts

services:
  web:
    extra_hosts:
      - "somehost:192.168.1.100"

Network

# creates a custom network called `frontend`
networks:
  frontend:

External network

# join a preexisting network
networks:
  default:
    external:
      name: frontend

References

Based off cheatsheet from https://devhints.io/docker-compose.

@samqi
Copy link
Author

samqi commented May 23, 2025

@samqi
Copy link
Author

samqi commented May 23, 2025

# update APT repositories and install standard security and apt software libraries for respoitories
sudo apt-get update
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg -y
#add key for docker - ubuntu repositories and add the respositories so that all future APT Update for Docker will use this rather than default Ubuntu Docker binaries
sudo su
# Add Docker's official GPG key:
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

exit

#https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/#install-docker-ce-1
#3. On production systems, you should install a specific version of Docker CE instead of always using the latest. 
#This output is truncated. List the available versions.
#old  ->sudo apt-get install docker docker-compose -y
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

#start and enable docker to autostart with system reboot
sudo systemctl start docker
sudo systemctl enable docker
sudo docker images

#allow your normal user administrator or ubuntu (aws) to use docker command without sudo
sudo usermod -aG docker $USER  
sudo setfacl -m user:$USER:rw /var/run/docker.sock 

#verify docker works
docker run hello-world

if setfacl does not work, install acl package on ubuntu
sudo apt install acl -y

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment