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 to | Use |
|---|---|
| Run one thing | docker run |
| Run several things that talk to each other | docker compose up |
| Keep data after a container is deleted | a volume (-v or volumes:) |
| Reach a container from your laptop | a published port (-p or ports:) |
01 /Images
| Command | What it does |
|---|---|
docker build -t name:1 . | Build an image from the Dockerfile in this folder and call it name:1. |
docker images | List images on your machine. |
docker pull python:3.11-alpine | Download an image from Docker Hub. |
docker rmi name:1 | Delete an image. |
docker history name:1 | Show 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| Flag | Meaning |
|---|---|
-d | Run in the background (detached). |
--name web | Give the container a name you can type later. |
-p 127.0.0.1:18110:8080 | Open 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:/data | Mount the volume mydata at /data inside the container. |
-e MODE=lab | Set an environment variable inside the container. |
--rm | Delete the container when it stops. |
-it | Interactive with a terminal. Use it to open a shell: docker run -it alpine sh. |
| Command | What it does |
|---|---|
docker ps | List running containers. Add -a to include stopped ones. |
docker logs web | Show what the container printed. -f follows live, --tail 20 shows the last 20 lines. |
docker exec web ls /app | Run one command inside a running container. |
docker exec -it web sh | Open a shell inside it. Type exit to leave. |
docker stop web / docker start web | Stop or start it. Data in the container stays until you remove it. |
docker rm -f web | Stop and delete the container. Its own files go with it. Volumes stay. |
docker inspect web | Full details as JSON: ports, health, mounts, IP address. |
docker stats | Live CPU and memory use of every running container. |
03 /Volumes and networks
| Command | What it does |
|---|---|
docker volume ls | List volumes. |
docker volume inspect mydata | Show where a volume lives and which project made it. |
docker volume rm mydata | Delete a volume and the data in it. |
docker network ls | List networks. Compose makes one per project. |
docker network inspect name | Show 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.
| Command | What it does |
|---|---|
docker compose up -d | Create and start everything in the background. Add --build to rebuild images, --wait to wait until healthchecks pass. |
docker compose ps | Show the state of each service, including health. |
docker compose logs web | Logs of one service. -f follows, --tail 20 limits. |
docker compose exec web sh | Open a shell in a running service. |
docker compose run --rm web cmd | Run a one-off container from a service, then remove it. |
docker compose config | Print the final configuration, with names and defaults filled in. Great for finding typos. |
docker compose restart web | Restart a service. Its container keeps its own files. |
docker compose up -d --force-recreate web | Delete and recreate a service's container. Only volumes survive. |
docker compose down | Remove containers and the network. Keeps volumes. |
docker compose down -v | Same, and also deletes volumes, which is your data. |
05 /Cleaning up
| Command | What it does |
|---|---|
docker system df | How much disk Docker is using. |
docker container prune | Delete all stopped containers. |
docker image prune | Delete images nothing uses. |
docker system prune | Delete 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 see | It usually means |
|---|---|
State running | The main process exists. It does not say the app is ready. |
(healthy) in docker ps | The 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. |