Skip to main content

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.

Check Docker is installed
docker --version
Docker version 24.x.x, build xxxxxxx
Verify Docker is running
docker info
Remember

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.

Download an image from Docker Hub
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
Remember

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:

List all local images
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest a]b1c2d3e4f5g 2 weeks ago 77.8MB
nginx latest 1234567890ab 3 days ago 187MB
Remember

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.

Run a container from an image
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​

Open a lunch box and step inside
docker run -it ubuntu bash
root@a1b2c3d4e5f6:/#

You're now inside the container's Ubuntu environment. Type exit to leave.

Common mistake

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​

Pack the lunch box and let it run on its own
docker run -d --name my-web nginx
a1b2c3d4e5f6g7h8i9j0...
  • -d = detached (runs in the background)
  • --name = give the container a friendly name
Remember

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:

List running containers
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx "/docker-entrypoint.…" 5 seconds ago Up 5 seconds 80/tcp my-web
List ALL containers (including stopped)
docker ps -a
Remember

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​

Stop a running container
docker stop my-web
Remove a stopped container
docker rm my-web
Force-remove a running container
docker rm -f my-web
Common mistake

You can't remove a running container without -f. Always stop first, then rm, or use rm -f.

Clean up everything​

Remove all stopped containers
docker container prune

Remove all unused images
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:

See the output of a background container
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
Follow the logs in real time
docker logs -f my-web
Remember

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?

Open a shell inside a running container
docker exec -it my-web bash
root@a1b2c3d4e5f6:/#
Run a single command without entering
docker exec my-web cat /etc/nginx/nginx.conf
Remember

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.

Dockerfile — The Recipe
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Build the image
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)
Remember

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:

docker-compose.yml
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:
Start everything at once
docker compose up -d
Stop everything
docker compose down
Remember

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:

Create a volume
docker volume create library-data
List volumes
docker volume ls
Use a volume in a container
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.

Remember

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:

Create a network
docker network create campus-net
Run containers on the same network
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.

Remember

Use bridge networks (the default) for single-host containers. Use overlay networks for multi-host (Docker Swarm).

Cheat Sheet​

CommandLunch box meaning
docker pullStock the kitchen shelf
docker imagesWhat's on the shelf?
docker runPack and open a lunch box
docker psWhat's cooking right now?
docker stopClose the lunch box
docker rmRecycle the lunch box
docker logsWhat did the kitchen say?
docker execOpen a packed lunch box
docker buildPack your own lunch box
docker compose upPack multiple lunch boxes at once
docker volumeStore leftovers
docker networkConnect lunch boxes