The Desi Architect
Notes / Docker Sandboxes / Docker and Compose Cheat Sheet
Track 06 · Docker Sandboxes · Reference

Docker and Compose Cheat Sheet

Every Docker and Compose command the labs use, what each flag means, and the dangerous ones to think twice about. Same commands on Windows, Mac and Linux.

Free referenceKeep this tab open while you do a lab

00 /The one idea

In plain words

An image is a packed, ready-to-run app. A container is a running copy of an image. A volume is a folder kept outside the container so data survives. Compose starts several containers from one file.

You want toUse
Run one thingdocker run
Run several things that talk to each otherdocker compose up
Keep data after a container is deleteda volume (-v or volumes:)
Reach a container from your laptopa published port (-p or ports:)

01 /Images

CommandWhat it does
docker build -t name:1 .Build an image from the Dockerfile in this folder and call it name:1.
docker imagesList images on your machine.
docker pull python:3.11-alpineDownload an image from Docker Hub.
docker rmi name:1Delete an image.
docker history name:1Show the layers of an image and their sizes.

02 /Containers

docker run -d --name web -p 127.0.0.1:18110:8080 -v mydata:/data -e MODE=lab --rm myimage:1
FlagMeaning
-dRun in the background (detached).
--name webGive the container a name you can type later.
-p 127.0.0.1:18110:8080Open port 18110 on your laptop and send it to port 8080 inside. 127.0.0.1 means only your laptop can reach it.
-v mydata:/dataMount the volume mydata at /data inside the container.
-e MODE=labSet an environment variable inside the container.
--rmDelete the container when it stops.
-itInteractive with a terminal. Use it to open a shell: docker run -it alpine sh.
CommandWhat it does
docker psList running containers. Add -a to include stopped ones.
docker logs webShow what the container printed. -f follows live, --tail 20 shows the last 20 lines.
docker exec web ls /appRun one command inside a running container.
docker exec -it web shOpen a shell inside it. Type exit to leave.
docker stop web / docker start webStop or start it. Data in the container stays until you remove it.
docker rm -f webStop and delete the container. Its own files go with it. Volumes stay.
docker inspect webFull details as JSON: ports, health, mounts, IP address.
docker statsLive CPU and memory use of every running container.

03 /Volumes and networks

CommandWhat it does
docker volume lsList volumes.
docker volume inspect mydataShow where a volume lives and which project made it.
docker volume rm mydataDelete a volume and the data in it.
docker network lsList networks. Compose makes one per project.
docker network inspect nameShow which containers are on a network and their private addresses.

04 /Docker Compose

Run these in the folder that holds docker-compose.yml. Use docker compose (with a space). The old docker-compose with a hyphen is the previous version.

CommandWhat it does
docker compose up -dCreate and start everything in the background. Add --build to rebuild images, --wait to wait until healthchecks pass.
docker compose psShow the state of each service, including health.
docker compose logs webLogs of one service. -f follows, --tail 20 limits.
docker compose exec web shOpen a shell in a running service.
docker compose run --rm web cmdRun a one-off container from a service, then remove it.
docker compose configPrint the final configuration, with names and defaults filled in. Great for finding typos.
docker compose restart webRestart a service. Its container keeps its own files.
docker compose up -d --force-recreate webDelete and recreate a service's container. Only volumes survive.
docker compose downRemove containers and the network. Keeps volumes.
docker compose down -vSame, and also deletes volumes, which is your data.

05 /Cleaning up

CommandWhat it does
docker system dfHow much disk Docker is using.
docker container pruneDelete all stopped containers.
docker image pruneDelete images nothing uses.
docker system pruneDelete stopped containers, unused networks and dangling images.
Read before you press Enter

docker compose down -v, docker volume rm, docker volume prune and docker system prune --volumes delete data for good. In a lab that is fine. On a real project, check which volumes would go first.

06 /Health and exit codes

You seeIt usually means
State runningThe main process exists. It does not say the app is ready.
(healthy) in docker psThe healthcheck passes.
(health: starting)Docker is still waiting for the first successful check.
Exited (0)The process finished on purpose.
Exited (1)The app crashed. Read docker logs.
Exited (137)The container was killed, usually for using too much memory. Give Docker Desktop more memory.