Docker Cheat Sheet 2026 — Commands, Dockerfile, and Compose

Bookmark this page. Use Ctrl+F (or Cmd+F on Mac) to find what you need. This cheat sheet covers Docker CLI commands, Dockerfile instructions, Docker Compose, volumes, networking, and production tips. Last updated: March 2026 How Docker Works ┌──────────────┐ docker build ┌──────────┐ docker run ┌────────────┐ │ Dockerfile │ ───────────────► │ Image │ ─────────────► │ Container │ │ (recipe) │ │ (template)│ │ (running) │ └──────────────┘ └──────────┘ └────────────┘ │ docker push │ ┌────▼───────┐ │ Registry │ │ (Docker Hub)│ └────────────┘ Container Commands Command Description docker run <image> Create and start a container docker run -d <image> Run in background (detached) docker run -it <image> bash Run interactively with shell (sh for Alpine) docker run -p 8080:80 <image> Map host port 8080 to container port 80 docker run --name myapp <image> Run with a custom name docker run --rm <image> Automatically remove container when it stops docker run -e KEY=value <image> Set environment variable docker run -v /host:/container <image> Mount a volume docker ps List running containers docker ps -a List all containers (including stopped) docker stop <container> Stop a running container docker start <container> Start a stopped container docker restart <container> Restart a container docker rm <container> Remove a stopped container docker rm -f <container> Force remove (even if running) docker exec -it <container> bash Open a shell inside a running container docker logs <container> View container logs docker logs -f <container> Follow logs in real-time docker inspect <container> Show detailed container info (JSON) docker stats Live resource usage for all containers docker cp <container>:/path ./local Copy file from container to host docker cp ./local <container>:/path Copy file from host to container docker container prune Remove all stopped containers docker kill <container> Force stop a container (SIGKILL) Image Commands Command Description docker images List all local images docker pull <image> Download an image from registry docker push <image> Upload an image to registry docker build -t myapp . Build image from Dockerfile in current dir docker build -t myapp:v1 . Build with a tag/version docker tag <image> <new-tag> Add a tag to an image docker rmi <image> Remove an image docker image prune Remove unused (dangling) images docker system prune -a Remove all unused images, containers, networks (not volumes) docker system prune -a --volumes Remove everything including volumes docker history <image> Show image layers and sizes docker login Log in to Docker Hub (required before push) docker logout Log out from registry docker save -o image.tar <image> Export image to tar file docker load -i image.tar Import image from tar file Dockerfile Reference # Base image FROM node:20-alpine # Set working directory WORKDIR /app # Copy dependency files first (for caching) COPY package.json package-lock.json ./ # Install dependencies RUN npm ci --production # Copy application code COPY . . # Expose a port (documentation only) EXPOSE 3000 # Default command when container starts CMD ["node", "server.js"] Dockerfile Instructions Instruction Description FROM <image> Base image (required, must be first) WORKDIR /app Set working directory for subsequent commands COPY <src> <dest> Copy files from host to image ADD <src> <dest> Like COPY but can extract tar and fetch URLs RUN <command> Execute command during build (creates a layer) ENV KEY=value Set environment variable ARG KEY=value Build-time variable (not available at runtime) EXPOSE <port> Document which port the app listens on CMD ["executable", "arg"] Default command (can be overridden) ENTRYPOINT ["executable"] Fixed command (CMD becomes arguments) VOLUME /data Create a mount point for persistent data USER <username> Switch to non-root user `HEALTHCHECK CMD curl -f http://localhost/ CMD vs ENTRYPOINT # CMD — default command, can be overridden CMD ["python", "app.py"] # docker run myapp → runs python app.py # docker run myapp bash → runs bash (overrides CMD) # ENTRYPOINT — fixed command, CMD becomes arguments ENTRYPOINT ["python"] CMD ["app.py"] # docker run myapp → runs python app.py # docker run myapp test.py → runs python test.py Multi-Stage Build # Stage 1: Build FROM node:20-alpine AS builder WORKDIR /app COPY package.json ./ RUN npm ci COPY . . RUN npm run build # Stage 2: Production (smaller image) FROM nginx:alpine COPY --from=builder /app/dist /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] Multi-stage builds produce smaller images — the final image only contains the production output, not the build tools. ...

March 19, 2026 · 6 min