Docker Commands Cheat Sheet

Your go-to reference for Docker CLI, Compose, and Dockerfile best practices.

Image Management

Build an image from a Dockerfile
docker build -t app-name:latest .
-t tag   -f specify file   --no-cache ignore cache
Pull an image from a registry
docker pull nginx:alpine
List local images
docker images
Remove an image
docker rmi image_id
Remove dangling/unused images
docker image prune -a

Container Lifecycle

Run a container (detached, with ports)
docker run -d -p 8080:80 --name myapp nginx
-d detached   -p host:container port   -v volume
List running containers
docker ps -a
-a include stopped containers
Stop a running container
docker stop container_id
Remove a stopped container
docker rm container_id
-f force removal (even if running)

Interacting & Executing

Execute a shell inside a container
docker exec -it container_id /bin/sh
-it interactive tty
View container logs
docker logs -f container_id
-f follow   --tail 100 last 100 lines
Display running processes in container
docker top container_id
Return low-level information (JSON)
docker inspect container_id

Docker Compose

Start all services in background
docker compose up -d
Stop and remove containers, networks
docker compose down
-v also remove named volumes
View logs for all compose services
docker compose logs -f
Build or rebuild services
docker compose build --no-cache

Networks & Volumes

List and create networks
docker network ls
docker network create my_net
List and create volumes
docker volume ls
docker volume create my_data
Clean up everything (system prune)
docker system prune -a --volumes
Removes stopped containers, unused networks/images, and volumes.

Dockerfile Basics

Base Image Definition
FROM node:18-alpine
Set Working Directory
WORKDIR /app
Copy files into image
COPY package*.json ./
COPY . .
Run build commands
RUN npm install --production
Define container startup command
CMD ["node", "app.js"]

Why Use This Docker Cheat Sheet?

Docker has fundamentally changed how developers build, ship, and run applications. However, remembering every command, flag, and configuration option in the Docker CLI can be challenging. This interactive reference guide is designed to provide quick, filterable access to the most common Docker commands and Dockerfile patterns.

Best Practices for Dockerfiles

  • Use Official Base Images: Start with official, minimal images like alpine or slim variants of Node/Python to reduce attack surface and download size.
  • Leverage Build Cache: Order your Dockerfile commands from least likely to change (like copying dependency manifests) to most likely to change (like source code).
  • Multi-stage Builds: Use multi-stage builds to compile code in one container and copy only the final artifacts into a smaller runtime container.
  • Don't Run as Root: Define a non-root user (e.g., USER node) for better security and isolation.

Understanding Container Lifecycle

A typical Docker workflow involves building an image (docker build), running a container instance from that image (docker run), viewing its outputs (docker logs), and eventually stopping and removing it (docker stop and docker rm). For multi-container applications, Docker Compose (docker compose up) simplifies orchestration by defining the entire stack in a single YAML file.