Docker Compose Cheat Sheet

Welcome to our Docker Compose Cheat Sheet !

overview
Dock­er-­Com­pose is used to define and run multi-­con­tainer Docker applic­ations. You use a YAML file to configure your applic­ation’s services. Then, with a single command, you can create and start all the services defined in the yml file.
docker-compose up is used to start docker compose defined services. Depending on the contents of the .yml file it may build the images, or it may start services from existing images on the docker host.
docker-compose up
docker­-co­mpose up uses docker­-co­mpo­se.yml to start services
docker­-co­mpose -f <fi­len­ame.ym­l> -f <fi­len­ame­loc­al.y­ml> up uses a yml file in a custom location/name to start services
-d, –detach runs the services in detached/background mode
–build forces image build from dockerfile before starting services
–no-buildbypasses the image build
–forc­e-r­ecreate force recreation of containers when starting services
–no-color outputs without colour
–scale lets you set the number of instances of the service
stopping and restarting
docker­-co­mpose start starts existing service/containers
docker-compose stopstops running services/containers but doesn’t delete them
docker­-co­mpose pause pauses running containers
docker­-co­mpose unpause unpauses running containers
docker-compose restartrestarts all stopped and running services.
docker­-co­mpose kill forces running containers to stop
docker­-co­mpose down stops containers and removes contai­ners, networks and images
docker­-co­mpose down –volumesstops containers and removes contai­ners, networks, images and volumes
remove stopped containers
docker-compose rmremoves stopped containers
docker-compose rm -fforces removal of containers
docker-compose rm -sstops the containers before removing
example docker-compose.yml file
version: ‘3.3’
services:
db:
image: mysql:5.7
volumes: – db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress

wordpress:
depends_on: – db
image: wordpress:latest
volumes: – web_data:/var/www/html
ports: – “8000:80”
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress

volumes:
db_data: {}
web_data: {}
more docker-compose cmds
docker-compose psshows list of containers for a service
docker-compose logsdisplays logs for a service
docker-compose pullpulls the images specified in the docker-compose.yml file but doesnt start any services
docker-compose buildbuilds images, but doesn’t start services
docker-compose runruns a command against a service
docker-compose versionoutputs the version of docker compose
docker-compose configView the docker-compose.yml file used to start the services
installing docker compose
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

$ sudo chmod +x /usr/local/bin/docker-compose

$ docker-compose

If you found this useful, please share!

Related posts

Mastering the Linux ifconfig Command

Docker Exec Command With Practical Examples

Debugging with Git Bisect

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More