Ultimate Docker Commands Cheat Sheet: Container Management and DevOps Reference

Ultimate Docker Commands Cheat Sheet

Ultimate Docker Commands Cheat Sheet

I still remember the first time “it works on my machine” stopped being a punchline and actually became a real problem I had to solve. Docker fixed that for me almost overnight — but only after I stopped guessing at commands and actually learned the tool properly. Since then, I’ve built this cheat sheet the same way I built the Git one: as the reference I genuinely keep open while I work, not a list copy-pasted from documentation.

This covers the full lifecycle — images, containers, volumes, networks, Compose, and the DevOps habits that separate “I got a container running” from “I run production workloads on this.” Whether you’re debugging a crashing container at 2 a.m. or setting up your first CI pipeline, this is meant to be the page you land on.

Table of Contents

  1. Docker Basics: Images vs Containers
  2. Installing and Verifying Docker
  3. Working with Images
  4. Running and Managing Containers
  5. Inspecting and Debugging Containers
  6. Dockerfile Essentials
  7. Volumes and Data Persistence
  8. Networking
  9. Docker Compose
  10. Docker Registry and Publishing Images
  11. Resource Limits and Performance
  12. Docker Swarm Basics
  13. Security Best Practices
  14. Troubleshooting Common Errors
  15. Real-World DevOps Workflows
  16. Common Mistakes to Avoid
  17. FAQs
  18. Interview Questions
  19. Printable Quick-Reference Summary
  20. Official Documentation Links

1. Docker Basics: Images vs Containers

Before the commands, the mental model that made everything click for me: an image is a read-only template — your application plus everything it needs to run, packaged into layers. A container is a running (or stopped) instance of that image. You can spin up ten containers from the same image, and each one runs in its own isolated process with its own filesystem layer on top. Images are the recipe; containers are the meal.

2. Installing and Verifying Docker

CommandDescription
docker --versionShows the installed Docker version
docker infoDisplays system-wide Docker info (containers, images, storage driver)
docker versionShows client and server (daemon) version details
docker run hello-worldRuns a test container to confirm Docker is working

Example:

docker run hello-world

Expected output (abbreviated):

Hello from Docker!
This message shows that your installation appears to be working correctly.

3. Working with Images

CommandDescription
docker pull <image>Downloads an image from a registry
docker pull <image>:<tag>Pulls a specific tagged version
docker imagesLists all local images
docker image lsSame as above (newer syntax)
docker build -t <name>:<tag> .Builds an image from a Dockerfile in the current directory
docker tag <image> <new-name>Tags an image with a new name
docker rmi <image>Removes a local image
docker image pruneRemoves unused (dangling) images
docker image prune -aRemoves all unused images, not just dangling ones
docker history <image>Shows the layer history of an image
docker save -o file.tar <image>Exports an image to a tar file
docker load -i file.tarImports an image from a tar file

Example:

docker build -t myapp:1.0 .

Expected output (abbreviated):

[+] Building 12.4s (10/10) FINISHED
 => [internal] load build definition from Dockerfile
 => => naming to docker.io/library/myapp:1.0

Real-world tip: Always tag images with a meaningful version instead of relying only on latest. When something breaks in production, latest tells you nothing about what changed.

4. Running and Managing Containers

CommandDescription
docker run <image>Creates and starts a new container
docker run -d <image>Runs in detached (background) mode
docker run -it <image> bashRuns interactively with a terminal attached
docker run -p 8080:80 <image>Maps host port 8080 to container port 80
docker run --name <name> <image>Assigns a custom container name
docker run -e VAR=value <image>Sets an environment variable
docker run --rm <image>Automatically removes the container when it stops
docker psLists running containers
docker ps -aLists all containers, including stopped ones
docker start <container>Starts a stopped container
docker stop <container>Gracefully stops a running container
docker restart <container>Restarts a container
docker kill <container>Forcefully stops a container immediately
docker rm <container>Removes a stopped container
docker rm -f <container>Force-removes a running container
docker container pruneRemoves all stopped containers

Example:

docker run -d --name webapp -p 3000:3000 -e NODE_ENV=production myapp:1.0

Expected output:

a1b2c3d4e5f6g7h8i9j0

(This is the container ID — Docker prints it on success.)

5. Inspecting and Debugging Containers

CommandDescription
docker logs <container>Shows container logs
docker logs -f <container>Streams logs live (like tail -f)
docker exec -it <container> bashOpens an interactive shell inside a running container
docker exec <container> <command>Runs a one-off command inside a container
docker inspect <container>Shows detailed JSON metadata about a container
docker statsShows live CPU, memory, and network usage for running containers
docker top <container>Shows running processes inside a container
docker diff <container>Shows filesystem changes made since the container started
docker cp <container>:<path> <host-path>Copies files from a container to the host
docker cp <host-path> <container>:<path>Copies files from the host into a container

Example:

docker exec -it webapp bash

Expected output: Drops you into an interactive shell inside the container, e.g. root@a1b2c3d4:/app#

Debugging habit I rely on constantly: when a container exits immediately, docker logs <container> is almost always the first place the answer is — before reaching for inspect or anything heavier.

6. Dockerfile Essentials

A Dockerfile is the blueprint Docker uses to build an image, line by line.

Example Dockerfile for a Node.js app:

FROM node:20-alpine

WORKDIR /app

COPY package*.json ./
RUN npm ci --only=production

COPY . .

EXPOSE 3000
ENV NODE_ENV=production

CMD ["node", "server.js"]
InstructionPurpose
FROMSets the base image
WORKDIRSets the working directory inside the container
COPYCopies files from the host into the image
ADDLike COPY, but also supports URLs and auto-extracting archives
RUNExecutes a command during the build (e.g., installing packages)
CMDDefault command run when the container starts (overridable)
ENTRYPOINTFixed command that always runs; args are appended, not replaced
EXPOSEDocuments which port the container listens on
ENVSets an environment variable in the image
ARGDefines a build-time-only variable
USERSets the user the container runs as
VOLUMEDeclares a mount point for persistent data
HEALTHCHECKDefines how Docker checks if the container is healthy

Best practice I always follow: order your Dockerfile so things that change least often (like COPY package*.json and RUN npm ci) come before things that change often (like COPY . .). Docker caches layers, so this ordering means most builds only re-run the final steps instead of reinstalling every dependency from scratch.

Multi-stage build example (keeps final images small):

FROM node:20 AS build
WORKDIR /app
COPY . .
RUN npm ci && npm run build

FROM node:20-alpine
WORKDIR /app
COPY --from=build /app/dist ./dist
CMD ["node", "dist/server.js"]

7. Volumes and Data Persistence

Containers are ephemeral by design — anything written inside them disappears when the container is removed, unless you use volumes.

CommandDescription
docker volume create <name>Creates a named volume
docker volume lsLists all volumes
docker volume inspect <name>Shows volume details
docker volume rm <name>Removes a volume
docker volume pruneRemoves all unused volumes
docker run -v <volume>:<path> <image>Mounts a named volume into a container
docker run -v $(pwd):/app <image>Bind-mounts a host directory into a container

Example:

docker volume create db-data
docker run -d --name postgres-db -v db-data:/var/lib/postgresql/data postgres:16

Expected output:

db-data

Rule of thumb: use named volumes for persistent application data (databases, uploads), and bind mounts mainly for local development when you want live code changes reflected instantly inside the container.

8. Networking

CommandDescription
docker network lsLists all Docker networks
docker network create <name>Creates a custom network
docker network inspect <name>Shows network details, including connected containers
docker network connect <network> <container>Connects a running container to a network
docker network disconnect <network> <container>Disconnects a container from a network
docker network rm <name>Removes a network
docker run --network <name> <image>Runs a container attached to a specific network

Example:

docker network create app-network
docker run -d --name api --network app-network myapi:1.0
docker run -d --name db --network app-network postgres:16

Containers on the same custom network can reach each other by container name (api can connect to db at hostname db) — Docker’s built-in DNS handles that automatically, which is much cleaner than hardcoding IP addresses.

9. Docker Compose

Compose is where Docker starts feeling like a real application platform instead of a single-container tool.

Example docker-compose.yml:

version: "3.9"
services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    depends_on:
      - db
  db:
    image: postgres:16
    environment:
      - POSTGRES_PASSWORD=secretpassword
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data:
CommandDescription
docker compose upBuilds and starts all services
docker compose up -dStarts services in detached mode
docker compose downStops and removes containers, networks
docker compose down -vAlso removes named volumes
docker compose psLists running services
docker compose logs -fStreams logs from all services
docker compose buildRebuilds images defined in the compose file
docker compose restart <service>Restarts a specific service
docker compose exec <service> bashOpens a shell inside a running service
docker compose configValidates and displays the resolved compose configuration

Example:

docker compose up -d

Expected output (abbreviated):

[+] Running 3/3
 ✔ Network myproject_default   Created
 ✔ Container myproject-db-1    Started
 ✔ Container myproject-web-1   Started

10. Docker Registry and Publishing Images

CommandDescription
docker loginLogs in to Docker Hub (or another registry)
docker login <registry-url>Logs in to a private registry
docker tag <image> <username>/<repo>:<tag>Tags an image for pushing
docker push <username>/<repo>:<tag>Pushes an image to a registry
docker pull <username>/<repo>:<tag>Pulls an image from a registry
docker logoutLogs out of the current registry

Example:

docker tag myapp:1.0 sarahdev/myapp:1.0
docker push sarahdev/myapp:1.0

Expected output (abbreviated):

The push refers to repository [docker.io/sarahdev/myapp]
1.0: digest: sha256:a1b2c3... size: 1571

11. Resource Limits and Performance

CommandDescription
docker run --memory="512m" <image>Limits container memory usage
docker run --cpus="1.5" <image>Limits CPU usage
docker update --memory="1g" <container>Updates resource limits on a running container
docker stats --no-streamShows a one-time snapshot of resource usage
docker system dfShows disk space used by images, containers, and volumes
docker system pruneRemoves unused containers, networks, and dangling images
docker system prune -a --volumesAggressive cleanup — removes everything unused, including volumes

I run docker system prune regularly on my dev machine — Docker is notorious for quietly eating disk space with old layers and stopped containers you forgot about.

12. Docker Swarm Basics

Swarm is Docker’s built-in orchestration tool — lighter-weight than Kubernetes, and worth knowing even if your team eventually moves to something bigger.

CommandDescription
docker swarm initInitializes a new swarm on the current node
docker swarm join --token <token> <ip>Joins a node to an existing swarm
docker node lsLists nodes in the swarm
docker service create --name <name> <image>Creates a new service
docker service lsLists running services
docker service scale <service>=<n>Scales a service to n replicas
docker service update --image <image> <service>Rolling-updates a service to a new image
docker stack deploy -c <compose-file> <stack-name>Deploys a full stack from a compose file

13. Security Best Practices

14. Troubleshooting Common Errors

ErrorLikely CauseFix
Cannot connect to the Docker daemonDocker service isn’t runningStart Docker Desktop or run sudo systemctl start docker
port is already allocatedAnother process or container is using that host portChange the host port mapping or stop the conflicting process
Error response from daemon: No such containerContainer name/ID is wrong or already removedCheck docker ps -a for the correct name/ID
Container exits immediately after docker runThe main process finished or crashed instantlyCheck docker logs <container>; ensure CMD/ENTRYPOINT runs a long-lived process
permission denied on a mounted volumeFile permission mismatch between host and container userAdjust ownership with chown, or match container user ID to host user
no space left on deviceDocker’s storage is full of unused images/layersRun docker system prune -a --volumes
Build is extremely slowPoor Dockerfile layer ordering, large build contextReorder Dockerfile, add a .dockerignore file

.dockerignore example (speeds up builds significantly):

node_modules
.git
*.log
.env
dist

15. Real-World DevOps Workflows

Local development loop:

docker compose up -d
docker compose logs -f web
# make code changes, container picks them up via bind mount
docker compose down

CI/CD build-and-push pipeline (conceptual, e.g., GitHub Actions):

docker build -t sarahdev/myapp:${{ github.sha }} .
docker tag sarahdev/myapp:${{ github.sha }} sarahdev/myapp:latest
docker push sarahdev/myapp:${{ github.sha }}
docker push sarahdev/myapp:latest

Zero-downtime deployment pattern with Swarm:

docker service update --image sarahdev/myapp:new-tag --update-parallelism 1 --update-delay 10s myapp-service

Debugging a production incident:

docker ps
docker logs --tail 200 -f <container>
docker exec -it <container> sh
docker stats <container>

16. Common Mistakes to Avoid

17. FAQs

Q: What’s the difference between an image and a container? An image is the static, read-only template. A container is a running instance created from that image, with its own writable layer on top.

Q: What’s the difference between CMD and ENTRYPOINT in a Dockerfile? CMD sets a default command that can be fully overridden when running the container. ENTRYPOINT sets a fixed command that always executes, with any extra arguments appended rather than replacing it. They’re often combined: ENTRYPOINT for the fixed binary, CMD for default arguments.

Q: Why did my container exit right after starting? Docker containers stay alive only as long as their main process (PID 1) keeps running. If that process finishes — or was never a long-running process to begin with — the container exits, even if the exit code is 0.

Q: Do I need Kubernetes if I’m already using Docker? Not necessarily. Docker Compose and Swarm handle small to mid-sized deployments fine. Kubernetes becomes worth the added complexity when you need advanced scheduling, multi-node orchestration at scale, or you’re already working in an environment that expects it.

Q: How is a bind mount different from a volume? A bind mount maps a specific host path into the container, so it’s tied to your local filesystem structure. A named volume is managed entirely by Docker, making it more portable and better suited for production data.

Q: Why is my image so large? Common causes are using a full OS base image instead of a slim/alpine variant, not cleaning up build dependencies, or copying unnecessary files because there’s no .dockerignore. Multi-stage builds fix most of this.

Q: Is Docker the same as a virtual machine? No. Containers share the host machine’s kernel and isolate at the process level, making them far lighter and faster to start than a full VM, which virtualizes an entire operating system.

18. Interview Questions

  1. Explain the difference between an image, a container, and a Dockerfile.
  2. What’s the purpose of multi-stage builds, and how do they reduce image size?
  3. How does Docker layer caching work, and how do you optimize a Dockerfile for it?
  4. Describe the difference between a bind mount and a named volume.
  5. How would you debug a container that keeps restarting?
  6. What’s the difference between docker stop and docker kill?
  7. How do containers on the same Docker network communicate with each other?
  8. What security risks come with running a container as root, and how do you avoid it?
  9. How would you reduce the size of a Node.js or Python Docker image?
  10. What’s the difference between Docker Compose and Docker Swarm?

19. Printable Quick-Reference Summary

IMAGES
  docker pull <image>
  docker build -t name:tag .
  docker images
  docker rmi <image>

CONTAINERS
  docker run -d -p host:container --name name image
  docker ps -a
  docker stop / start / restart <container>
  docker rm <container>

DEBUGGING
  docker logs -f <container>
  docker exec -it <container> bash
  docker inspect <container>
  docker stats

VOLUMES
  docker volume create <name>
  docker run -v name:/path image

NETWORKS
  docker network create <name>
  docker run --network name image

COMPOSE
  docker compose up -d
  docker compose down
  docker compose logs -f
  docker compose exec service bash

REGISTRY
  docker login
  docker tag image user/repo:tag
  docker push user/repo:tag

CLEANUP
  docker system prune -a --volumes
  docker image prune
  docker container prune

20. Official Documentation Links


That’s the reference I actually use — from docker run hello-world on day one to debugging a service mid-incident years later. Keep it close, and don’t feel like you need to memorize every flag. The commands you use daily will stick on their own; this page is for the ones you only need once every few months and always forget.

Exit mobile version