intermediate devops Docker 26 · Updated April 2026

Docker Essentials Cheatsheet

A comprehensive guide to Docker for developers, covering essential commands, core concepts, and best practices for Docker 26.

· 8 min read · AI-reviewed
-->

Quick Overview

Docker is an open-source platform that enables developers to build, ship, and run applications in isolated environments called containers. It packages an application and all its dependencies into a standardized unit, ensuring it runs consistently across different computing environments. Docker is crucial for modern development, facilitating faster deployment, simplified scaling, and consistent environments from development to production. This cheatsheet covers Docker version 26.

Install Docker Desktop:

# macOS, Windows, Linux Desktop environments:
# Download from official Docker website: https://docs.docker.com/desktop/install/

Getting Started

To get started with Docker 26, you’ll need to install Docker Desktop for your operating system. Docker Desktop bundles Docker Engine, Docker CLI client, Docker Compose, Kubernetes, and Notary.

Installation

macOS:

  1. Download the Docker Desktop for Mac installer (e.g., Docker.dmg) from the official Docker website.
  2. Double-click Docker.dmg and drag the Docker icon to your Applications folder.
  3. Open Docker.app from your Applications folder. You may need to accept the Docker Subscription Service Agreement.

Windows:

  1. Ensure WSL 2 (Windows Subsystem for Linux 2) is enabled. If not, open PowerShell as administrator and run wsl --install then wsl --update.
  2. Download the Docker Desktop for Windows installer (Docker Desktop Installer.exe).
  3. Double-click the installer and follow the wizard. Ensure “Use WSL 2 instead of Hyper-V” is selected during configuration.
  4. Launch Docker Desktop after installation.

Linux (Ubuntu Example): For Ubuntu 22.04, 24.04, or the latest non-LTS, Docker Desktop is recommended.

  1. Set up Docker’s apt repository:
    # Update package lists
    sudo apt-get update
    # Install necessary packages for apt to use HTTPS repositories
    sudo apt-get install ca-certificates curl gnupg
    # Add Docker's GPG key
    sudo install -m 0755 -d /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    # Add Docker repository to Apt sources
    sudo chmod a+r /etc/apt/keyrings/docker.gpg
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    # Update package lists again after adding the repository
    sudo apt-get update
  2. Install Docker Desktop:
    sudo apt install ./docker-desktop-amd64.deb
    # (Replace with the correct architecture if not amd64)
    Alternatively, install Docker Engine directly:
    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  3. Add your user to the docker group to run Docker commands without sudo (requires a log out/in):
    sudo usermod -aG docker ${USER}
  4. Launch Docker Desktop from your applications menu.

Hello World

Verify your installation by running the hello-world container.

# Pulls the "hello-world" image and runs it as a container
docker run hello-world

If successful, you’ll see a message confirming Docker is working correctly.

Core Concepts

Understanding these fundamental concepts is key to mastering Docker:

ConceptDescription
ImageA lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files. Images are built from Dockerfiles.
ContainerA runnable instance of an image. You can create, start, stop, move, or delete a container. It’s an isolated process running on the host machine, sharing the host OS kernel but isolated from other containers and the host OS.
DockerfileA text file that contains all the commands a user could call on the command line to assemble an image. It’s a “recipe” for creating Docker images.
VolumeThe recommended mechanism for persisting data generated by and used by Docker containers. Volumes are entirely managed by Docker and are stored in a part of the host filesystem (e.g., /var/lib/docker/volumes/ on Linux).
Bind MountA way to persist data by mounting a file or directory from the host machine directly into a container. Docker doesn’t manage their lifecycle, and they can be stored anywhere on the host filesystem.
NetworkEnables communication between containers, the host, and external networks. Docker provides several network drivers (bridge, host, overlay, none). bridge is the default for isolated containers on the same host.
RegistryA repository for Docker images. Docker Hub is the default public registry, but private registries can also be used. Images are pushed to and pulled from registries.

Essential Commands / API / Syntax

The 80/20 reference for common Docker tasks.

Image Management

Images are the blueprints for containers.

Container Management

Containers are running instances of images.

Volume Management

Manage persistent data for containers.

Network Management

Manage how containers communicate.

Docker Compose Commands (as of Docker 26, docker compose is the standard)

Docker Compose is for defining and running multi-container Docker applications.

Common Patterns

1. Building a Multi-stage Dockerfile

Multi-stage builds are crucial for creating small, secure, and efficient images by separating build-time dependencies from runtime dependencies.

Example: Node.js application

# Stage 1: Build the application
FROM node:20-alpine AS builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build

# Stage 2: Create the final lightweight runtime image
FROM node:20-alpine
WORKDIR /app
# Copy only the necessary build artifacts from the 'builder' stage
COPY --from=builder /app/build ./build
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json

# Expose the port your app runs on
EXPOSE 3000

# Command to run the application
CMD ["npm", "start"]

Explanation: The builder stage includes Node.js, npm, and all development dependencies to build the application. The final stage then copies only the essential build output and node_modules into a fresh, minimal Node.js runtime image, significantly reducing the final image size and attack surface.

2. Running a Multi-container Application with Docker Compose

Define an entire application stack (e.g., a web service and a database) in a single compose.yaml file.

compose.yaml example:

# Use version 3 of the Compose file format
version: '3.8'

services:
  web:
    # Build image from Dockerfile in current directory
    build: .
    # Map host port 80 to container port 80
    ports:
      - "80:80"
    # Mount named volume 'web-data' to /app/data in container
    volumes:
      - web-data:/app/data
    # Link to the 'db' service
    depends_on:
      - db
    # Environment variables
    environment:
      DATABASE_URL: postgres://user:password@db:5432/mydatabase

  db:
    image: postgres:16-alpine
    # Mount named volume 'db-data' for persistent database storage
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    # Expose port only within the Docker network
    expose:
      - "5432"

volumes:
  web-data:
  db-data:

To run this application:

# Start the application stack
docker compose up -d

This command builds the web service image, pulls the postgres image, creates the necessary networks and volumes, and starts both services.

3. Persistent Data with Volumes for Databases

Using named volumes is the recommended way to persist database data, ensuring data survives container restarts and removals.

# Create a named volume for PostgreSQL data
docker volume create my-postgres-data

# Run a PostgreSQL container, mounting the named volume
docker run -d --name my-postgres \
  -p 5432:5432 \
  -v my-postgres-data:/var/lib/postgresql/data \
  -e POSTGRES_DB=mydatabase \
  -e POSTGRES_USER=myuser \
  -e POSTGRES_PASSWORD=mypassword \
  postgres:16-alpine

The data directory /var/lib/postgresql/data inside the container is now backed by the my-postgres-data volume on the host.

Gotchas & Tips


Source: z2h.fyi/cheatsheets/docker-cheatsheet — Zero to Hero cheatsheets for developers.