DockerPlay
1

Docker Command Cheat Sheet

Searchable, battle-tested reference library for core Docker commands with 1-click execution support.

ContainersCreate and start a new container from an image
docker run -d --name web -p 8080:80 -e ENV=prod nginx:alpine

Combines `docker create` and `docker start` in a single command. Provisions a read-write container layer and boots the process.

Watch out: Forgetting `-d` and having the container block your active terminal, or reversing port order in `-p`.
Pro Tip: Use `--rm` for one-off CLI tools (e.g. `docker run --rm node:22 node -v`) so containers automatically clean up when done.
ContainersList active and running containers
docker ps -a

Displays running containers, their ports, status, and names. Adding `-a` reveals stopped and exited containers.

Watch out: Thinking a container disappeared when it simply stopped. Always check `docker ps -a` to find exited containers.
Pro Tip: Use `docker ps -q` to output only container IDs (great for scripting like `docker stop $(docker ps -q)`).
ContainersGracefully stop a running container
docker stop web-app

Sends SIGTERM signal to PID 1 inside the container, waits up to 10 seconds for graceful shutdown, then sends SIGKILL if still running.

Watch out: Using `docker kill` when a graceful shutdown with `docker stop` is preferred for database data safety.
Pro Tip: You can stop multiple containers at once: `docker stop web api db`.
ContainersRemove one or more stopped containers
docker rm -f web-app

Deletes the container and its ephemeral read-write filesystem layer. Mounted volumes are not deleted.

Watch out: Trying to remove a running container without `-f` or without stopping it first.
Pro Tip: Use `docker container prune` to delete all stopped containers at once.
ImagesDownload an image from a container registry (e.g. Docker Hub)
docker pull postgres:16-alpine

Fetches image layer blobs and manifest from a remote registry into your local engine cache.

Watch out: Omitting the tag and accidentally pulling `:latest`, which may introduce unexpected breaking changes over time.
Pro Tip: Always pin explicit semantic tags (e.g. `node:22.11-alpine`) in production.
ImagesBuild an image from a Dockerfile
docker build -t myapp:1.0 .

Sends the build context (directory containing Dockerfile) to the Docker Daemon and executes steps to produce image layers.

Watch out: Forgetting the trailing dot (`.`) specifying the current directory build context.
Pro Tip: Create a `.dockerignore` file to exclude `node_modules`, `.git`, and build artifacts from the build context.
ImagesList local cached images
docker images

Displays repository names, tags, image IDs, creation timestamps, and virtual disk sizes.

Watch out: Confusing image size with container RAM usage. Image size is disk space.
Pro Tip: Use `docker image prune -a` to clean up unused dangling images.
NetworkingCreate a user-defined bridge network
docker network create app-net

Sets up an isolated virtual bridge with embedded DNS name resolution for automatic container discovery.

Watch out: Relying on the default `bridge` network and wondering why containers cannot ping each other by name.
Pro Tip: Custom networks automatically enable DNS resolution between containers using their container names.
NetworkingConnect a container to an existing network
docker network connect backend-net frontend-container

Dynamically attaches an extra network interface to a running or stopped container without restarting it.

Watch out: Assuming a container can only belong to one network. Containers can be multi-homed to bridge distinct zones.
Pro Tip: Use multi-network setups to create secure DMZs (e.g. frontend on public & backend net, db only on backend net).
VolumesCreate a named persistent volume
docker volume create pgdata

Allocates managed storage on the host filesystem that lives outside container lifecycles.

Watch out: Deleting a container and expecting volume data to be gone, or forgetting to mount the volume to a new container.
Pro Tip: Named volumes are faster and more portable across OSes than host bind mounts.
Docker ComposeBuild, create, and start multi-container services
docker compose up -d --build

Reads `compose.yaml`, creates required networks, volumes, pulls/builds images, and starts services with dependency ordering.

Watch out: Forgetting `-d` and accidentally terminating the stack when closing terminal.
Pro Tip: Add `--build` if you made source code changes and need images recompiled.
Debugging & LogsView stdout and stderr output from a container
docker logs -f --tail 100 web-server

Streams captured output written to PID 1 inside the container.

Watch out: Wondering why logs are empty if the application logs to a file inside the container instead of stdout/stderr.
Pro Tip: Always design containerized applications to log to standard output (`/dev/stdout`).
Debugging & LogsRun a command inside an active container
docker exec -it web-app sh

Spawns a new process inside the running container namespaces (great for interactive debugging, curl tests, inspecting env).

Watch out: Trying to exec into a stopped container. The container must be in `running` state.
Pro Tip: If `bash` is not found, try `sh` since minimal Alpine images only bundle `/bin/sh`.
Sponsored Resources