Skip to content
Michael's Dev Blog

My Docker(-Compose) Cheatsheet

Docker, Docker-Compose, Cheatsheet3 min read

Docker Guide Title Image

Most web applications require multiple services, which need to work in parallel. One for the database, one for the Django web server, one for the background tasks and so on.

The proper manual setup, configuration and documentation of all those services will get complicated very quickly. Therefore it's a good idea to automate these steps with Docker and Docker-Compose.

This article is basically a cheatsheet containing my most used docker commands.

Terminology 🧑‍🏫

This section will explain the following terms:

  • Docker
  • Image
  • Dockerfile
  • Container
  • Volumes
  • Docker-Compose

Docker Architecture Docker Architecture (Source: https://docs.docker.com/get-started/overview/#docker-architecture)

Docker enables us to create lightweight virtual machines on our desktop that behaves like a full separate computer. The setup and configuration of those virtual machines can be automated by using Images and Dockerfiles. Images are blueprints or a read-only template with instructions for creating a Docker container.

We can think of Docker Containers as lightweight virtual machines or a runnable instances of an image. We can build, start, stop, move, or delete a container using the Docker API or CLI. We can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

Next, I have to explain Volumes. Volumes enables us to mount a folder in the physical file system of our desktop (=host) machine into the virtual file system of a Docker container. So what happens is that when the container writes data to it's filesystem it gets replicated or automatically written on the host file system and vice versa. This is extremely useful during development, because any changes to the source code files will be immediately visible in the Docker container. 💪

Next, we need a program called Docker-Compose. It's very common for applications to consist of multiple containers/services. Manually starting and stopping all those containers/services can be very annoying and is prone to errors. This is where Docker-Compose comes into play. Compose is a tool for defining and running multi-container Docker applications. With Compose, we use a YAML file to configure our application’s services. Then, with a single command, we create and start all the services from our configuration.

Summary 💡

  • Docker enables us to create lightweight virtual machines (=containers)
  • Images are blueprints or templates for these containers
  • Dockerfiles can be used to build images automatically by reading the instructions.
  • Container is a runnable instance of the image (=lightweight virtual machine)
  • Volumes allow us to share files between the host and the container
  • Docker-Compose is a tool for managing multi-container Docker applications.

Prerequisites / Installation

Make sure Docker and Docker-Compose are installed on your machine by typing docker-compose version into your terminal program. It should show you the installed version of Docker Compose. Otherwise please follow the official Docker Compose Install Guide for your operating system.

Note for Ubuntu users:

  1. Install Docker as descriped here
  2. Do the post install steps
  3. Install Docker-Compose as descriped here

Docker Command Guide 🚀

This is a summary of my most used docker command line commands:

Generic

Images

Containers

Volumes

Examples

1# display docker version to test proper installation
2docker --version
3
4# display help (please note that the --help flag works on every docker command)
5docker --help
6
7################################# IMAGES #######################################
8
9# List all images that are locally stored with the Docker Engine
10docker images --all
11
12# Pull an image called `python:alpine` from the official Docker Hub
13# (see: https://hub.docker.com/_/python)
14docker pull python:alpine
15
16# Delete/remove the image with id `2c31ca135cf9` from the local image store
17docker rmi 2c31ca135cf9
18
19
20################################## CONTAINERS ##################################
21
22# List all containers
23docker ps --all
24
25# Create and start a new docker container from an image called `python:alpine`
26# (Note: the flags `-i` and `-t` can be combined to `-it`)
27# (If you want to detach from a running container, try `Ctrl + d` or `Ctrl + c`)
28docker run -it python:alpine
29
30# Delete/Remove the container with id `661f5cfb0881`
31docker rm 661f5cfb0881
32
33
34################################# CLEAN-UP #####################################
35
36# Stop all running containers
37docker stop $(docker ps -a -q)
38
39# Show a summary of the space currently used by different docker objects.
40# (df stands for disk free)
41docker system df
42
43# Remove unused data (this command is considered to be safe)
44# This will remove:
45# - stopped containers
46# - unused networks (not used by at least one container)
47# - unused volumes (not used by at least one container)
48# - unused images (not referenced by any container)
49# - build cache
50docker system prune --all --volumes
51
52################################ DANGER-ZONE ###################################
53
54# Delete ALL containers (running and stopped containers)
55docker rm -f $(docker ps -qa)
56
57# Delete ALL images (used and unused)
58docker rmi -f $(docker images -q)
59
60# Delete ALL volumes (used and unused)
61docker volume rm $(docker volume ls -q)

Docker-Compose Guide 🚀

Basic Usage

With Compose, we use a YAML file to configure your application’s services.

1# `docker-compose.yml` example file
2version: "3.9" # optional since v1.27.0
3services:
4 web:
5 build: .
6 ports:
7 - "80:8000"
8 volumes:
9 - .:/code
10 depends_on:
11 - db
12 db:
13 image: postgres
14 volumes:
15 - db-data:/var/lib/postgresql/data
16volumes:
17 db-data: {}

Let's break down the code from above:

1# specify the Compose file version for backward compatibility.
2version: "3.9"
3
4# define two services called `web` and `db`.
5services:
6 web:
7 # Set build options ("context" and Dockerfile).
8 # A build’s context is the set of files located in the specified path or
9 # url. The build process can refer to any of the files in the context.
10 # For example, your build can use a `COPY` instruction to reference a
11 # file in the context.
12 # (see: https://docs.docker.com/engine/reference/commandline/build/)
13 #
14 # current directory is used in this case.
15 build: .
16
17 # map TCP port 80 in the host system to port 8000 on the container.
18 # Host_Port:80 <==> Container_Port:8000
19 ports:
20 - "80:8000" # HOST:CONTAINER
21
22 # Mount host paths or named volumes, specified as sub-options to a service.
23 volumes:
24 # The short syntax is uses the generic SOURCE:TARGET format.
25 # SOURCE can be either a host path or volume name
26 # TARGET is the container path where the volume is mounted
27 #
28 # current directory on the host machine is mounted to `/code` directory
29 # in the container.
30 - .:/code # HOST_PATH:MOUNTED_CONTAINER_PATH
31
32 # Express dependency between services
33 # `docker-compose up` starts services in dependency order.
34 #
35 # `db` is started before `web`.
36 depends_on:
37 - db
38 db:
39 # Build the service from an image called `postgres`
40 # see: https://hub.docker.com/_/postgres
41 image: postgres
42
43 # Please check out the generic volume description in the `web` service.
44 volumes:
45 # Mount the named volume `db-data`.
46 - db-data:/var/lib/postgresql/data # VOLUME_NAME:MOUNTED_CONTAINER_PATH
47
48# Defines a named volume called `db-data`.
49#
50# The contents of that volume (even if they are changed via a process in the
51# container) will persist through the containers lifecycle even when stopped/removed.
52volumes:
53 db-data:

CLI Commands

1# Builds, (re)creates, starts, and attaches to containers for a service.
2docker-compose up
3
4# Stops running containers without removing them.
5docker-compose stop
6# (They can be started again with docker-compose start.)
7docker-compose start
8
9# Run a one-time command against a service. For example, the following command
10# starts the `web` service and runs `bash` as its command.
11docker-compose run web bash
12
13################################ DANGER-ZONE ###################################
14
15# Stop containers and remove containers, networks, volumes, and images created
16# by `up`.
17docker-compose down

Troubleshooting 😠

"Low Disk Space on Filesystem root"

One day I got the warning "low disk space on filesystem root", which was something I did not expect. I realized that under Ubuntu images are stored in the root directory (/var/lib/docker/). My main problem was, that I had some large (unused) images lying around which consumed my disk space.

You can pull some images and run docker images to see their actual image size. Here are some examples:

Screenshot of the Docker image stats

If you run into disk space constraints please consider deleting some images and/or try to use smaller image versions like the slim or alpine tag of the python image.