6 20 Real 15
Categories Commands Scenarios Quiz Qs
■ Contents Docker Cheatsheet
1 Container Basics docker run · docker ps · docker stop · docker exec
2 Images docker build · docker pull/push · docker images
3 Volumes & Storage docker volume create · bind mounts
4 Networking docker network create · inspect
5 Docker Compose compose up · compose down/logs
6 Logs & Debugging docker logs · docker stats · docker inspect
7 Quiz 15 questions to test your Docker knowledge
■ Container Basics 4 commands
docker run -d -p 8080:80 nginx CMD
WHAT IT DOES
Creates and starts a new container from an image. -d runs it in the background (detached), -p maps host port 8080 to container port
80. The most used Docker command.
■ REAL-WORLD SCENARIO
You pulled an nginx image and want to start it as a web server, running in the background so it keeps running after you close the terminal.
$ docker run -d -p 8080:80 --name my-nginx nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
Status: Downloaded newer image for nginx:latest
Container ID: a1b2c3d4e5f6 (running in background)
docker ps / docker ps -a CMD
WHAT IT DOES
Lists all currently running containers. Add -a to see ALL containers including stopped ones. Shows container ID, image, status, ports,
and name.
■ REAL-WORLD SCENARIO
Your application isn't responding. You run docker ps -a to check if the container is running, stopped, or exited with an error code.
$ docker ps -a
CONTAINER ID IMAGE STATUS PORTS NAMES
a1b2c3d4e5f6 nginx Up 2 hours [Link]:8080->80 my-nginx
b2c3d4e5f6a7 mysql Exited (1) 5m my-db
docker stop / docker rm CMD
WHAT IT DOES
stop gracefully stops a running container by sending SIGTERM. rm removes a stopped container. Use rm -f to force remove a running
container in one step.
■ REAL-WORLD SCENARIO
You deployed a new version of your app and need to stop the old container and remove it before starting the updated one.
$ docker stop my-app && docker rm my-app
my-app
my-app
# Or force remove in one command:
$ docker rm -f my-app # stopped and removed instantly
docker exec -it container bash CMD
WHAT IT DOES
Opens an interactive terminal session inside a running container. -i keeps stdin open, -t allocates a terminal. Essential for debugging
inside containers.
■ REAL-WORLD SCENARIO
Your containerized app is throwing errors but logs aren't detailed enough. You exec into the container to inspect config files or check env
variables.
$ docker exec -it my-nginx bash
root@a1b2c3d4e5f6:/# ls /etc/nginx/
conf.d/ [Link] sites-available/
root@a1b2c3d4e5f6:/# cat /etc/nginx/[Link]
# You are now inside the container
■■ Images 3 commands
docker build -t name:tag . CMD
WHAT IT DOES
Builds a Docker image from a Dockerfile in the current directory. -t tags the image with a name and version. The dot means use the
current directory as build context.
■ REAL-WORLD SCENARIO
You finished writing your application and its Dockerfile. You need to build it into an image before you can run it as a container or push it to a
registry.
$ docker build -t my-api:v1.0 .
Step 1/6 : FROM node:18-alpine
Step 2/6 : WORKDIR /app
Step 3/6 : COPY [Link] .
Successfully built image my-api:v1.0
docker pull / docker push CMD
WHAT IT DOES
pull downloads an image from Docker Hub or a private registry. push uploads your local image to a registry so others or your servers
can use it.
■ REAL-WORLD SCENARIO
You built your application image locally and need to push it to Docker Hub so your server can pull and run it during deployment.
$ docker tag my-api:v1.0 vishnutomar/my-api:v1.0
$ docker push vishnutomar/my-api:v1.0
v1.0: digest: sha256:abc123 size: 1234
# On the server:
$ docker pull vishnutomar/my-api:v1.0
docker images / docker rmi CMD
WHAT IT DOES
images lists all locally stored images with their names, tags, IDs, and sizes. rmi removes an image. Use docker image prune to remove
all unused images.
■ REAL-WORLD SCENARIO
Your server's disk is running low. You check which old Docker images are taking up space and remove the ones no longer needed to free
gigabytes.
$ docker images
REPOSITORY TAG IMAGE ID SIZE
my-api v2.0 abc123def456 245MB
my-api v1.0 def456abc123 243MB ← old
$ docker rmi my-api:v1.0 # Deleted: sha256:def456
■ Volumes & Storage 2 commands
docker volume create / ls CMD
WHAT IT DOES
Creates a named volume for persistent storage. Unlike bind mounts, Docker manages named volumes. Data survives even when the
container is deleted and restarted.
■ REAL-WORLD SCENARIO
You are running a MySQL database in a container. Without a volume, all your data disappears when the container stops. You create a volume
so data persists.
$ docker volume create mysql-data
$ docker run -d \
-v mysql-data:/var/lib/mysql \
--name mysql-db mysql:8.0
$ docker volume ls # DRIVER: local NAME: mysql-data
docker run -v /host:/container CMD
WHAT IT DOES
Bind mount maps a directory from your host machine directly into the container. Changes on either side are reflected immediately —
great for development workflows.
■ REAL-WORLD SCENARIO
You are developing a [Link] application and want live code reloading inside the container without rebuilding the image every time you change
a file.
$ docker run -d \
-v $(pwd)/src:/app/src \
-p 3000:3000 my-node-app
# Edit files in ./src → changes appear instantly
# No rebuild needed during development
■ Networking 2 commands
docker network create CMD
WHAT IT DOES
Creates a custom network so containers can communicate by name instead of IP address. Containers on the same network can reach
each other directly.
■ REAL-WORLD SCENARIO
You have a web app container and a database container. Without a custom network they can't talk to each other. You create a network and
connect both.
$ docker network create app-network
$ docker run -d --network app-network --name my-db mysql:8.0
$ docker run -d --network app-network --name my-app my-api:v1.0
# my-app can now reach my-db by hostname 'my-db'
docker network ls / inspect CMD
WHAT IT DOES
ls lists all networks. inspect shows detailed info about a network including which containers are connected and their IP addresses.
■ REAL-WORLD SCENARIO
Your app container says it cannot connect to the database. You inspect the network to verify both containers are on the same network and
check their IPs.
$ docker network inspect app-network
{
'Containers': {
'my-db': { 'IPv4Address': '[Link]/16' },
'my-app': { 'IPv4Address': '[Link]/16' }
■ Docker Compose 2 commands
docker compose up -d CMD
WHAT IT DOES
Starts all services defined in [Link] in detached mode. Automatically creates networks, volumes, and containers. The
standard way to run multi-container apps.
■ REAL-WORLD SCENARIO
Your project has a web server, API, database, and Redis cache. Instead of four separate docker run commands, you define everything in
compose and start with one command.
# [Link] defines: web, api, db, redis
$ docker compose up -d
Creating network app_default
Creating db ... done Creating redis ... done
Creating api ... done Creating web ... done
docker compose down / logs CMD
WHAT IT DOES
down stops and removes all containers, networks, and volumes created by compose up. logs shows combined output of all services —
add -f to follow live.
■ REAL-WORLD SCENARIO
A service is failing and you want to see logs from all containers at once to find the error, then cleanly stop everything.
$ docker compose logs -f api
api_1 | Server started on port 8080
api_1 | ERROR: Redis connection refused
# Found the issue — Redis isn't ready yet
$ docker compose down # Stops and removes all
■ Logs & Debugging 3 commands
docker logs -f container CMD
WHAT IT DOES
Fetches the log output of a container. -f follows the log stream in real time, like tail -f for containers. Add --tail 100 to see only the last N
lines.
■ REAL-WORLD SCENARIO
Your containerized application just crashed in production. You immediately pull up the logs to see the last error messages before the container
exited.
$ docker logs -f --tail 50 my-api
[2026-05-19 07:45] INFO API server started
[2026-05-19 07:46] INFO DB connection established
[2026-05-19 08:02] ERROR Cannot allocate memory
[2026-05-19 08:02] FATAL Process exited with code 137
docker stats CMD
WHAT IT DOES
Displays a live stream of CPU, memory, network, and disk usage for all running containers. Like htop but specifically for Docker
containers.
■ REAL-WORLD SCENARIO
Your server is running slowly and you suspect one container is consuming too many resources. Run docker stats to see which container is the
CPU or memory hog.
$ docker stats
CONTAINER CPU% MEM USAGE / LIMIT NET I/O
my-api 2.5% 245MiB / 2GiB 100MB
my-db 0.8% 512MiB / 2GiB 50MB
my-cache 0.1% 45MiB / 2GiB 10MB
docker inspect container CMD
WHAT IT DOES
Returns detailed low-level JSON about a container or image — IPs, mounts, env vars, network settings, restart policy, and much more.
■ REAL-WORLD SCENARIO
You need to find the exact IP address of a container on a custom network, or check which environment variables were passed when it was
started.
$ docker inspect my-api | grep IPAddress
'IPAddress': '[Link]'
$ docker inspect my-api | grep -A5 Env
'Env': ['DB_HOST=my-db', 'DB_PORT=5432',
'NODE_ENV=production']
■ Quiz — Test Your Docker Knowledge 15 Questions
Cover the answers and test yourself. Great for interview prep and team onboarding.
QUESTION QUESTION
1 2
What is the difference between a Docker image and a What does the -d flag do in docker run?
container?
✓ ANSWER
✓ ANSWER Runs the container in detached (background) mode so it keeps
An image is a read-only template (the blueprint). A container is a running after you close the terminal and you get your prompt
running instance of that image — like a recipe vs the actual dish. back immediately.
QUESTION QUESTION
3 4
How do you see all containers including stopped What is the purpose of -p 8080:80 in docker run?
ones?
✓ ANSWER
✓ ANSWER Maps port 8080 on the HOST to port 80 inside the CONTAINER.
docker ps -a — the -a flag shows ALL containers. Without it you Requests to localhost:8080 are forwarded into the container on
only see currently running containers. port 80.
QUESTION QUESTION
5 6
What is the difference between a volume and a bind Why do you need Docker volumes for databases?
mount?
✓ ANSWER
✓ ANSWER Without a volume, all data inside the container is lost when it
A named volume is managed by Docker and persists stops or is removed. Volumes ensure data persists across
independently. A bind mount maps a specific host directory into container restarts and removals.
the container — ideal for live dev reloading.
QUESTION QUESTION
7 8
What command opens a terminal inside a running What does docker compose up -d do?
container?
✓ ANSWER
✓ ANSWER Starts all services in [Link] in the background.
docker exec -it <name> bash — exec runs a command inside a Automatically creates networks, volumes, and starts containers
running container; -it makes it interactive with a terminal. in the correct order.
QUESTION QUESTION
9 10
How do containers on the same Docker network What is docker stats used for?
communicate?
✓ ANSWER
✓ ANSWER Shows a live stream of resource usage — CPU, memory,
Using container names as hostnames. If two containers share a network I/O — for all running containers. Like htop specifically for
network, one can reach the other simply by using its container Docker.
name as the host address.
QUESTION QUESTION
11 12
What does docker image prune do? What is the difference between docker stop and
docker kill?
✓ ANSWER
Removes all dangling (unused) images not referenced by any ✓ ANSWER
container. Helps free disk space on servers where many images stop sends SIGTERM giving the container time to shut down
accumulate over time. gracefully. kill sends SIGKILL which terminates immediately.
Always prefer stop unless the container is frozen.
QUESTION QUESTION
13 14
What command shows detailed config of a container What does docker compose down do vs just stopping
including IP and env vars? containers?
✓ ANSWER ✓ ANSWER
docker inspect — returns a detailed JSON object with all It stops AND removes containers, networks, and optional
low-level config: network settings, environment variables, volumes created by compose up. Just stopping leaves containers
mounts, and more. and networks in place — down does a full cleanup.
QUESTION
15
What is a Dockerfile and what is it used for?
✓ ANSWER
A text file with instructions for building a Docker image. It defines
the base image, copies files, installs dependencies, and sets the
startup command.
Containers changed how we deploy software.
Master Docker — and Kubernetes starts to make sense.
Follow Vishnu Singh Tomar on LinkedIn
for weekly Cloud & DevOps cheatsheets