Docker Commands — The Kitchen Inventory
Back with our five friends — Riya, Arjun, Maya, Kabir, and Simran — who are building a college library app called Campus Library. They've been sending recipes (code) to each other, but the food always tastes different on different machines. Now they're using Docker — the magic lunch box — to pack the entire kitchen.
0 · Setup — Installing the Lunch Box
Before anyone can pack a lunch box, Docker needs to be on your computer.
docker --version
Docker version 24.x.x, build xxxxxxx
docker info
Docker needs the Docker Engine (the daemon) running in the background. If docker info fails, start Docker Desktop or the Docker service.
1 · docker pull — Stocking the Kitchen Shelf
Before you can cook, you need ingredients. A Docker image is a pre-packed kitchen — it contains the OS, libraries, runtime, and everything your app needs. Pulling an image downloads it to your computer.
docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
Digest: sha256:abc123...
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
docker pull is like restocking your kitchen shelf. You do it once; the image stays until you remove it.
2 · docker images — What's on the Shelf?
See every image you have downloaded:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest a]b1c2d3e4f5g 2 weeks ago 77.8MB
nginx latest 1234567890ab 3 days ago 187MB
Images are the recipes. Containers are the actual lunches made from those recipes.
3 · docker run — Packing and Opening a Lunch Box
This is the most important command. docker run takes an image and starts a container — an actual running instance of that kitchen.
docker run ubuntu echo "Hello from the lunch box"
Hello from the lunch box
The container ran echo, printed the message, and stopped. That's a one-shot lunch box.
Run it interactively
docker run -it ubuntu bash
root@a1b2c3d4e5f6:/#
You're now inside the container's Ubuntu environment. Type exit to leave.
Forgetting -it means you can't interact with the container. You'll see it start and immediately stop, and wonder what happened.
Run it in the background
docker run -d --name my-web nginx
a1b2c3d4e5f6g7h8i9j0...
-d= detached (runs in the background)--name= give the container a friendly name
docker run = docker create + docker start. It's the all-in-one "make me a lunch" command.
4 · docker ps — What's Cooking Right Now?
Check which containers are currently running:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx "/docker-entrypoint.…" 5 seconds ago Up 5 seconds 80/tcp my-web
docker ps -a
docker ps without -a only shows running containers. Stopped ones are hidden. Use -a to see everything.
5 · docker stop / docker rm — Closing and Recycling the Lunch Box
docker stop my-web
docker rm my-web
docker rm -f my-web
You can't remove a running container without -f. Always stop first, then rm, or use rm -f.
Clean up everything
docker container prune
docker image prune -a
6 · docker logs — What Did the Kitchen Say?
If a container is running in the background, you can't see its output. docker logs retrieves it:
docker logs my-web
/docker-entrypoint.sh: Configuration complete; ready for start up
172.17.0.1 - - [10/Oct/2024:12:00:00 +0000] "GET / HTTP/1.1" 200
docker logs -f my-web
docker logs is your debugging tool. If something's wrong, check the logs first.
7 · docker exec — Opening a Packed Lunch Box
Need to run a command inside an already-running container?
docker exec -it my-web bash
root@a1b2c3d4e5f6:/#
docker exec my-web cat /etc/nginx/nginx.conf
docker exec is like opening a lunch box that's already packed and taking a bite. You don't need to unpack the whole thing.
8 · docker build — Packing Your Own Lunch Box
The real power: create your own image from a recipe file called Dockerfile.
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
docker build -t campus-library .
[+] Building 12.3s (8/8) FINISHED
=> [1/4] FROM node:20-alpine
=> [2/4] WORKDIR /app
=> [3/4] COPY package*.json ./
=> [4/4] RUN npm install
=> [5/4] COPY . .
=> => naming to docker.io/library/campus-library
-t campus-library= tag (name) the image.= the build context (current folder)
docker build reads the Dockerfile step by step. Each instruction becomes a layer in the image — layers are cached and reused.
9 · docker-compose — Packing Multiple Lunch Boxes at Once
When your app has multiple services (web server + database + cache), docker-compose manages them all:
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: secret
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
docker compose up -d
docker compose down
docker-compose is the lunch box carrier — it holds multiple lunch boxes together, each running its own service.
10 · docker volume — Storing Leftovers
Containers are ephemeral — when removed, their data is gone. Volumes persist data outside the container:
docker volume create library-data
docker volume ls
docker run -d \
--name my-db \
-v library-data:/var/lib/postgresql/data \
postgres:16
Now even if you remove the container, the data in library-data survives.
Volumes are the Tupperware — they keep leftovers (data) safe even after the lunch box (container) is thrown away.
11 · docker network — Connecting Lunch Boxes
By default, containers can't talk to each other. Networks let them communicate:
docker network create campus-net
docker run -d --name my-web --network campus-net nginx
docker run -d --name my-db --network campus-net postgres:16
Now my-web can reach my-db by its name: my-db:5432.
Use bridge networks (the default) for single-host containers. Use overlay networks for multi-host (Docker Swarm).
Cheat Sheet
| Command | Lunch box meaning |
|---|---|
docker pull | Stock the kitchen shelf |
docker images | What's on the shelf? |
docker run | Pack and open a lunch box |
docker ps | What's cooking right now? |
docker stop | Close the lunch box |
docker rm | Recycle the lunch box |
docker logs | What did the kitchen say? |
docker exec | Open a packed lunch box |
docker build | Pack your own lunch box |
docker compose up | Pack multiple lunch boxes at once |
docker volume | Store leftovers |
docker network | Connect lunch boxes |