Free Docker cheat sheet

Docker cheat sheet

Docker on one page: images, containers, the Dockerfile, docker compose, and the cleanup commands you forget.

ImagesRun containersManage containersA basic Dockerfiledocker compose (multi-container)Clean up

← All cheat sheets

Docker Cheat Sheet

Quick reference from FreeCodingCourses.com

Images

docker pull node:20
download an image from a registry.
docker build -t myapp .
build an image from the Dockerfile here, tag it myapp.
docker images
list local images.
docker rmi myapp
remove an image.

Run containers

docker run myapp
start a container from an image.
docker run -p 3000:3000 myapp
map host port 3000 to container port 3000.
docker run -d myapp
detached: run in the background.
docker run -it ubuntu bash
interactive shell inside the container.
docker run --env-file .env myapp
pass environment variables.

Manage containers

docker ps
running containers. docker ps -a includes stopped.
docker logs -f <id>
view and follow a container's output.
docker exec -it <id> bash
open a shell in a running container.
docker stop <id> docker start <id>
stop and restart.
docker rm <id>
delete a stopped container.

A basic Dockerfile

FROM node:20
start from a base image.
WORKDIR /app
set the working directory inside the image.
COPY package*.json ./ RUN npm ci
copy deps first, install, so this layer caches.
COPY . .
copy the rest of your code.
CMD ["node", "server.js"]
the command that runs when the container starts.

docker compose (multi-container)

docker compose up -d
start everything in compose.yaml, detached.
docker compose down
stop and remove the containers.
docker compose logs -f
tail logs from all services.
docker compose ps
status of the compose services.

Clean up

docker system prune
remove stopped containers, unused networks, dangling images.
docker image prune -a
remove all unused images. Frees a lot of disk.
docker volume ls
list volumes (your persistent data lives here).

Unlock the full Docker cheat sheet

You're seeing 2 of 6 sections. Drop your email to open the rest plus the gotchas, and save the whole thing as a printable PDF.

No spam, unsubscribe anytime. See our privacy policy.

Docker questions, answered

What is the difference between an image and a container?

An image is a read-only template: your app plus everything it needs to run, frozen. A container is a running instance of an image. One image can start many containers, the same way one class can create many objects.

Why use Docker at all?

Docker packages your app with its exact environment, so it runs the same on your laptop, a teammate's machine, and the server. It ends the it works on my machine problem and makes deploying and running multiple services much simpler.

Is it docker-compose or docker compose?

Both exist. The older standalone tool was docker-compose with a hyphen. Modern Docker builds it in as a subcommand, docker compose with a space. The commands and the YAML file are otherwise the same; prefer the space version on new setups.

Where to go next