Blog post

Docker is a powerful platform for building, packaging, distributing, and running applications in lightweight, isolated containers. I put together this cheatsheet because, with so many Docker commands to remember, I often found myself forgetting the exact syntax or mixing up options. This guide collects the essential and advanced commands I use most frequently, along with clear explanations and real-world examples. My goal is to make it easy to quickly refresh your memory, understand what each command does, and apply it confidently. Whether you're working with container lifecycles, networking, volumes, or multi-service setups with Docker Compose, I've organized everything in a clean, structured format that's easy to scan and use.

1. Docker System / Info Commands

docker --version
Prints the Docker client version installed on the system. Useful for verifying installation, PATH configuration, and CI environment validation.

docker version
Shows both the Docker client and server (daemon) version, including API compatibility, architecture, kernel, and build metadata.

docker info
Returns full diagnostic data: container count, image count, storage drivers, network settings, logging drivers, volumes, registries, CPU/memory stats.

2. Docker Image Commands

docker pull <image>
Downloads an image from a registry. Reuses existing local layers to optimize speed.
Example: docker pull nginx

docker pull <image>:<tag>
Pulls a specific image version. Ensures exact reproducibility across deployments.
Example: docker pull node:20

docker images
Lists all images stored locally, including repository, tag, size, and creation date.
Flags:
  • -a : show intermediate layers.
  • --digests : display image digests.
Example: docker images --digests

docker build .
Builds an image using the Dockerfile in the current directory. Sends context to the daemon.
Example: docker build .

docker build -t <name> .
Builds an image and applies a repository name and tag.
Flags:
  • -t : label the built image.
  • -f : specify a custom Dockerfile.
  • --build-arg : define build-time variables.
Example: docker build -t myapp:latest .

docker rmi <image>
Removes an image from local storage. Cannot delete if containers depend on it unless forced.
Flags:
  • -f : force removal.
Example: docker rmi nginx

3. Docker Container Commands

docker run <image>
Creates and runs a new container from an image.
Example: docker run nginx

docker run -it <image>
Runs a container interactively with a TTY. Ideal for shells and debugging.
Flags:
  • -i : interactive mode.
  • -t : allocate terminal.
Example: docker run -it ubuntu bash

docker run -d <image>
Runs a container in the background, returning a container ID immediately.
Flags:
  • -d : detached mode.
Example: docker run -d redis

docker run -p host:container <image>
Maps host ports to container ports, exposing services externally.
Flags:
  • -p : publish port.
Example: docker run -p 8080:80 nginx

docker logs <container>
Shows logs produced by a container's running processes.
Flags:
  • -f : follow logs.
  • --tail N : show last N lines.
Example: docker logs -f api

docker exec -it <container> sh
Runs a command inside a running container. Ideal for shell access.
Flags:
  • -i : interactive.
  • -t : TTY.
Example: docker exec -it alpine sh

4. Docker Volume Commands

docker volume create <name>
Creates a named persistent Docker volume managed by the Docker daemon. Useful for storing database files or other persistent data.
Example: docker volume create dbdata

docker volume ls
Lists all Docker-managed volumes on the host.

docker volume inspect <name>
Shows detailed metadata such as driver, mountpoint, labels, and scope in JSON format.
Example: docker volume inspect dbdata

docker volume rm <name>
Deletes a volume. The operation is irreversible and deletes stored data. Volumes cannot be removed if they are in use by containers.
Example: docker volume rm dbdata

docker volume prune
Removes all unused volumes. Useful for cleanup on systems with heavy build/test cycles that leave orphaned volumes.
Flags:
  • -f : skip confirmation prompt.
Example: docker volume prune -f

5. Docker Network Commands

docker network ls
Lists all Docker networks including bridge, host, none, and user-defined networks.

docker network create <name>
Creates a user-defined bridge network, enabling automatic container name resolution and isolated communication.
Example: docker network create backend

docker network inspect <name>
Displays JSON metadata such as subnet information, gateway, connected containers, and driver details.
Example: docker network inspect backend

docker network connect <network> <container>
Attaches a container to an existing network. Containers can belong to multiple networks for layered connectivity.
Example: docker network connect backend api

docker network disconnect <network> <container>
Detaches a container from a network. Useful for isolation or migration to another network segment.
Example: docker network disconnect backend api

6. Docker Registry Commands

docker login
Logs into a registry. Required before pushing or pulling private images. Uses credential helpers when available.
Flags:
  • -u : username.
  • -p : password (avoid using inline; prefer prompt).
Example: docker login ghcr.io

docker logout
Logs out of a registry by removing stored credentials.
Example: docker logout ghcr.io

docker tag <source> <target>
Creates a new reference (name/tag) for an existing image. Needed to assign registry paths before pushing.
Example: docker tag myapp ghcr.io/me/myapp:v1

docker push <image>
Uploads an image and its layers to a registry. Reuses already-present layers to optimize transfer.
Example: docker push ghcr.io/me/myapp:v1

7. Docker Compose Commands

docker compose up
Creates and starts all services defined in docker-compose.yml or compose.yml. Runs in foreground unless detached.
Flags:
  • -d : run services in detached mode.
  • --build : rebuild images before launching.
Example: docker compose up --build

docker compose down
Stops and removes all containers, networks, and default resources created by docker compose up.
Flags:
  • -v : remove volumes defined in the stack.
  • --rmi all : remove all images built by Compose.
Example: docker compose down -v

docker compose ps
Lists containers created within the Compose project, including state and published ports.

docker compose logs
Shows logs across all services or a chosen service in a Compose project.
Flags:
  • -f : follow logs live.
  • --tail : limit the number of lines shown.
Example: docker compose logs -f api

docker compose build
Builds or rebuilds images defined in your Compose configuration, respecting build context.
Flags:
  • --no-cache : build without using the layer cache.
  • --pull : attempt pulling newer base images.
Example: docker compose build --no-cache

docker compose exec <service> sh
Runs a command inside a running service container. Commonly used to open shells or run administrative commands.
Flags:
  • -no-tty : disable TTY allocation.
Example: docker compose exec app sh

docker compose restart
Restarts one or all services in a Compose application stack.
Example: docker compose restart api

8. Docker System Cleanup

docker system df
Displays disk usage for images, containers, volumes, and build cache. Useful before and after pruning operations.
Example: docker system df

docker system prune
Removes unused containers, networks, dangling images, and build cache. Does not remove volumes unless specified.
Flags:
  • -f : skip confirmation.
  • --volumes : also remove unused volumes.
Example: docker system prune -f

docker system prune -a
Performs aggressive cleanup by deleting all unused images, not just dangling ones. Useful for shrinking CI/CD containers but may require re-pulling images.
Flags:
  • -a : remove all unused images.
  • -f : skip confirmation.
Example: docker system prune -a -f

docker container prune
Removes all stopped containers to free disk space and reduce clutter.
Flags:
  • -f : skip prompt.
Example: docker container prune -f

docker image prune -a
Removes unused images (not referenced by any container). Helps reduce disk usage on build servers.
Flags:
  • -a : remove all unused images.
  • -f : skip confirmation.
Example: docker image prune -a -f

docker volume prune
Deletes all unused volumes. Cleans up old data directories left from failed or ephemeral containers.
Flags:
  • -f : skip prompt.
Example: docker volume prune -f

9. Advanced Docker Commands

docker save -o file.tar <image>
Exports one or more images as a tar archive. Useful for offline transfer or air-gapped environments.
Flags:
  • -o : output file.
Example: docker save -o app.tar myapp:latest

docker load -i file.tar
Loads images from a tar archive produced by docker save. Restores all tags and layers.
Flags:
  • -i : input tar file.
Example: docker load -i app.tar

docker export <container> > file.tar
Exports a container's filesystem as a tar archive without history or metadata. Used to capture a point-in-time root filesystem.
Example: docker export api > api.tar

docker import file.tar
Creates a new Docker image from a tarred root filesystem, such as those produced by docker export or third-party rootfs bundles.
Example: docker import api.tar

docker stats
Provides real-time usage metrics for running containers including CPU, memory, block I/O, network I/O, and PIDs.
Flags:
  • --no-stream : output only one snapshot instead of continuous streaming.
Example: docker stats

docker top <container>
Shows running processes inside a container using the host's process inspection tools.
Example: docker top api

10. Docker Context Commands

docker context ls
Lists all contexts, which define endpoints and environments for Docker. Useful when working with multiple remote Docker hosts.
Example: docker context ls

docker context create <name>
Creates a new context with its own Docker host settings, certificates, and connection parameters.
Example: docker context create dev-server

docker context use <name>
Switches the active Docker context. All subsequent Docker commands target this environment.
Example: docker context use dev-server