DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

Docker · revision

Docker cheat sheet

Recall first: read each stage's one-line summary and try to remember its commands. Then open the stage to check yourself.

15 stages32 commands

01IntroductionA container packages an isolated process with its filesystem and configuration. It starts quickly and shares the host kernel, which makes it different from a virtual machine.
  • docker run hello-world

    Pull and run Docker's introductory container image

  • docker images

    List locally available images

Remember

  • An image is the template; a container is a running instance.
  • Containers share the host kernel rather than booting a guest kernel.

Watch for A container is not a VM; isolation does not mean it has a separate kernel.

Open the lesson: Containers: what they are and why they won →
02Under the hoodDocker asks Linux to isolate process views with namespaces, limit resource use with cgroups, and assemble image layers with a writable container layer.
  • docker run -d --name cow alpine:3.22 sleep 300

    Start a small container process to inspect from the host and container

  • docker inspect -f '{{.State.Pid}}' cow

    Read the container process ID as seen by the host

Remember

  • Namespaces control what a process can see; cgroups account for and limit resources.
  • Overlay filesystems share read-only image layers and add a writable container layer.

Watch for The container's PID 1 is not necessarily PID 1 on the host.

Open the lesson: Under the hood: namespaces, cgroups and layered filesystems →
03InstallationChoose Docker Desktop for a managed local environment or install Docker Engine from Docker's signed repository on Linux. Use rootless mode when the default daemon access is too powerful.
  • docker version

    Check that the Docker client can reach a Docker Engine

  • sudo install -m 0755 -d /etc/apt/keyrings

    Create the directory used for the repository signing key

Remember

  • The Docker CLI and daemon are separate; the CLI must be able to reach the engine.
  • Membership in the docker group grants root-equivalent access to the host.

Watch for Don't add an account to the docker group as if it were an unprivileged permission.

Open the lesson: Installing Docker: Desktop, Engine on Linux, and rootless →
04Docker basicsPull an image, start a container, publish only the port you need, inspect its state and logs, then stop and remove it when the exercise is done.
  • docker run -d --name web -p 8080:80 nginx:1.30-alpine

    Start nginx and publish its container port 80 on host port 8080

  • curl -sI localhost:8080 | head -2

    Check the HTTP response from the published port

Remember

  • docker ps shows running containers; docker ps -a includes stopped containers.
  • Published ports connect a host port to a container port.

Watch for A stopped container still exists; remove it separately when you no longer need it.

Open the lesson: Docker basics: images, containers, ports, logs and exec →
05Third-party imagesBorrow images for databases, one-shot tools and language runtimes instead of installing each dependency on the host. Keep data and credentials out of disposable containers.
  • docker run --rm -v "$PWD":/work -w /work python:3.13-slim python total.py

    Run a Python script from a temporary runtime image against the current folder

  • docker run --rm -i ghcr.io/jqlang/jq:1.8.1 '[.orders[].price] | add'

    Run jq from an image and pass input on standard input

Remember

  • --rm removes a one-shot container after it exits.
  • A bind mount shares a host path; -w selects the working directory inside the container.

Watch for Bind-mount only the files the tool needs; the container can access mounted host data.

Open the lesson: Other people's images: databases, tools and runtimes →
06Data persistenceThe writable container layer disappears with the container. Store durable database files in a named volume, source code in a bind mount, and disposable scratch data in tmpfs.
  • docker volume create pgdata

    Create a named volume for database files

  • docker exec db psql -h 127.0.0.1 -U postgres -c "select t from notes"

    Verify that data remains available after replacing the database container

Remember

  • Named volumes are managed by Docker; bind mounts use a specific host path.
  • A backup is useful only after you have restored it and verified the data.

Watch for Recreating a container does not preserve data kept only in its writable layer.

Open the lesson: Keeping data: volumes, bind mounts and backups →
07Building imagesBuild from a Dockerfile and order instructions so stable dependencies stay cached. A multi-stage build can leave compilers and source out of the final runtime image.
  • docker build -t price-api:single -f Dockerfile.single .

    Build the image from the selected Dockerfile and tag it

  • docker images price-api

    Compare the locally built image sizes

Remember

  • Copy dependency manifests before frequently changed application source to preserve cache hits.
  • Copy only the runtime artifact from a build stage into the final stage.

Watch for A broad build context can send secrets and unrelated files to the builder; use .dockerignore.

Open the lesson: Building images: Dockerfiles, the layer cache and multi-stage builds →
08RegistriesA registry stores images for CI and deployment hosts. Log in to the intended registry, name images with the right namespace, and use immutable digests when a deployment must not drift.
  • docker tag price-api:multi localhost:5000/price-api:1.0.0

    Add a registry-qualified tag to the built image

  • docker push localhost:5000/price-api:1.0.0

    Push the tagged image to the local registry

Remember

  • Tags are mutable names; a digest identifies image content.
  • A registry path includes the registry host and repository name.

Watch for A tag alone does not prove which image content a later pull will return.

Open the lesson: Registries: Docker Hub, GHCR, ECR and tags you can trust →
09Running containersTreat containers as services: set restart behavior and resource limits, pass configuration at runtime, and describe multi-container dependencies in Compose.
  • docker run -d --name flaky --restart on-failure:3 alpine:3.22 sh -c 'echo starting; exit 1'

    Demonstrate a bounded restart policy for a failing process

  • docker compose up -d

    Start the services described in the Compose file

Remember

  • A restart policy responds to process exit; it does not make an unhealthy app healthy.
  • Resource limits constrain CPU, memory or process counts at runtime.

Watch for Restart loops can hide the original failure; inspect logs and exit state before raising retries.

Open the lesson: Running containers well: restart policies, limits, config and Compose →
10NetworkingUser-defined networks give containers name-based discovery and isolation. Publish a port only when traffic must cross from the host or outside network into a container.
  • docker network ls

    List Docker networks

  • docker network inspect bridge --format '{{range .IPAM.Config}}{{.Subnet}} gw {{.Gateway}}{{end}}'

    Inspect the default bridge network's subnet and gateway

Remember

  • Docker's embedded DNS resolves container and service names on user-defined networks.
  • Containers on separate user-defined networks cannot communicate unless connected to a shared network.

Watch for EXPOSE documents a port; -p actually publishes it from the host.

Open the lesson: Networking: bridge networks, published ports and container DNS →
11SecurityHarden images, runtime identity, Linux capabilities and secret handling as separate layers. Scan the exact image you intend to run, then reduce its privileges and contents.
  • docker run --rm alpine:3.22 id

    Check the default user inside the image

  • docker run --rm --user 1000:1000 alpine:3.22 sh -c 'id; touch /etc/x'

    Test a container process running as a non-root UID/GID

Remember

  • A non-root process reduces the impact of a compromised application.
  • Build-time secrets can leak into image layers or history; use the lesson's secret mount approach.

Watch for A secret passed through an environment variable can be visible to process inspection and diagnostics.

Open the lesson: Container security: scanning, non-root, runtime limits and secrets →
12Docker CLIFilter and format Docker's image, container, volume and network listings to answer operational questions. Measure disk use before pruning anything.
  • docker image ls --filter reference='price-api*' --format '{{.Repository}}:{{.Tag}} {{.Size}}'

    List matching images with their repository, tag and size

  • docker system df

    Measure Docker disk use before cleanup

Remember

  • Filters narrow output; --format makes it suitable for scripts.
  • Prune commands remove unused objects, so inspect what is selected first.

Watch for docker system prune can remove resources you meant to keep; review its scope before confirming.

Open the lesson: The Docker CLI day to day: inspect, filter, format and clean up →
13Developer experienceUse bind mounts and Compose watch for fast feedback, attach a debugger when needed, and run tests in the same image environment CI uses.
  • docker compose watch

    Start Compose's file-watch workflow for development

  • curl -s localhost:3000

    Check the app after a watched source change

Remember

  • Bind mounts make host source changes visible to a container.
  • Matching local and CI test environments reduces environment-specific failures.

Watch for Mounting over a container path hides the files that were already at that path in the image.

Open the lesson: Developing with Docker: hot reload, debugging, tests and CI →
14Deploying containersChoose a deployment home based on the team's operating needs: a managed container platform, Swarm or Kubernetes. Keep one image artifact across those targets.
  • docker swarm init --advertise-addr 127.0.0.1

    Initialize a local Swarm manager for the lesson's example

  • docker service ls

    List services running in the Swarm

Remember

  • A Swarm service declares desired replicas and update behavior.
  • Kubernetes adds a broader declarative orchestration API and ecosystem.

Watch for docker run starts a container; docker service create asks Swarm to maintain a service.

Open the lesson: Deploying containers: PaaS, Docker Swarm and Kubernetes →
15Back to your pipelineThe image part of a pipeline: tests run in a Dockerfile stage, the runtime image is scanned, only main pushes, the exact scanned image is pushed, and the server runs it by digest with a health-checked deploy that rolls back.
  • docker build --target test .

    Build only the test stage, which fails on a failing test

  • docker build --target runtime -t shop-api:ci .

    Build the image you ship

  • trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 shop-api:ci

    Fail on fixable HIGH or CRITICAL vulnerabilities

  • docker inspect --format '{{index .RepoDigests 0}}' localhost:5000/shop-api:test

    Read the digest of a pushed image

Remember

  • Push the image you scanned; never rebuild after the scan.
  • Tags are for people; a digest always means the same bytes.
  • compose up --wait fails when the container is not healthy, so the deploy can roll back.

Watch for Registry names must be lowercase, so lowercase GITHUB_REPOSITORY before using it in an image name.

Open the lesson: Back to your pipeline: test, scan, push and deploy the image →
← Back to the Docker course