Cheat sheets

Cheat sheet: Docker

The Docker and Compose commands you need every day. Replace container with the name or ID of your container.

Containers

View running containers docker ps
View all containers, including stopped ones docker ps -a
Start a container docker start container
Stop a container docker stop container
Restart a container docker restart container
Remove a container docker rm container
Clean up stopped containers docker container prune
Start a new container in the background docker run -d --name web -p 8080:80 nginx
View logs docker logs -f container
The last 100 lines of logs docker logs --tail 100 container
Open shell docker exec -it container bash
Open shell if there’s no bash docker exec -it container sh
Live CPU and memory usage docker stats
All details of a container docker inspect container
Copy a file to the container docker cp file container:/path/
Get a file from the container docker cp container:/path/file .

Images

View images docker images
Download an image docker pull nginx:latest
Build an image from a Dockerfile docker build -t mijn-app .
Remove an image docker rmi image
Clean up unused images docker image prune -a

Storage and network

View volumes docker volume ls
Create a volume docker volume create data
Clean up unused volumes (careful!) docker volume prune
View networks docker network ls
Create a network docker network create mijn-net

Docker Compose

Start everything in the background docker compose up -d
Stop and remove everything docker compose down
Only stop docker compose stop
Status of the services docker compose ps
Follow logs of all services docker compose logs -f
Logs of one service docker compose logs -f service
Pull new images docker compose pull
Update: pull and restart docker compose pull && docker compose up -d
Restart a service docker compose restart service
Shell in a service docker compose exec service sh
Check the compose file docker compose config

Cleanup and overview

How much space does Docker use? docker system df
Clean up everything unused (careful!) docker system prune
Information about the installation docker info
Show the version docker version

Good to know

  • docker system prune and docker volume prune delete data. Check first with docker system df and back up important volumes.
  • Ports that you publish bypass the UFW firewall: see the UFW cheat sheet.
  • More explanation: our Docker guides.

Sources and attribution

This overview is compiled in our own words. Commands and options change per version: when in doubt, check the official documentation.