PLAN-101 Career OS
Intermediate 45 Minutes

Docker Images, Containers and Reproducible Builds

Understand why an image differs from a running container, then package a tiny app without embedding secrets.

Key Learning Objectives

  • Distinguish immutable image layers from running containers.
  • Explain a Dockerfile layer cache.
  • Avoid placing secrets in image layers.
  • Design a reproducible container run command.

Separate the recipe, artifact and process

A Dockerfile is a build recipe, an image is the packaged artifact, and a container is a runtime instance of that image. A container has a writable layer and a lifecycle; deleting a container can discard files written only to that layer. Persist important data using an explicit volume or external database. Many containers can be started from the same image without changing its build instructions.

Make builds predictable and small

Docker processes build steps in layers. Copying package manifests and installing dependencies before copying frequently edited application code can preserve dependency-layer cache hits. Pin a tested runtime tag, use a .dockerignore file to avoid copying node_modules or private files, and choose a supported base image. Never copy .env files into the image; environment variables and platform-managed secrets are supplied when the container runs.

FROM node:22-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY server.js ./
USER node
EXPOSE 3000
CMD ["node", "server.js"]

This intentionally simple example uses locked dependencies and a non-root user; build with a matching lockfile and test your app’s file permissions.

Debug the process, not just the image

If a container exits unexpectedly, examine its logs and exit code. A successful build only proves that the image was assembled; it does not prove that your server listens on the correct address or that its dependencies are reachable. Map ports deliberately, add a health endpoint, and verify that persistent data survives removing and recreating the container.

Build It: Containerized study-notes API

Package a small HTTP API and verify it responds and persists a saved note after container recreation.

Project Deliverables

  • Dockerfile and .dockerignore.
  • Run instructions with port mapping.
  • A named volume or external storage setup.

Acceptance Criteria

  • Health endpoint returns success.
  • No credentials are baked into the image.
  • A saved note persists across container recreation.

Next Steps & Related Learning

This interactive lesson is part of the Docker curriculum. Continue exploring or try another interactive lesson:

View Full Docker → Arrays, Contiguous Memory & Hash Tables → JavaScript Asynchronous Execution & V8 Event Loop → Relational Database Architecture, B-Trees & Query Optimization → Semantic HTML, Accessible Forms & Document Structure → Python Functions, Dictionaries and Reliable Data Processing → SQL Joins, Missing Rows and Query Correctness → React State, Events and Derived UI → Designing a Task API: HTTP Methods, Errors and Validation → Sliding Window: From Quadratic Loops to a Linear Scan → Responsive CSS Layout: Flexbox, Grid and Fluid Constraints → TypeScript Union Types and Runtime Narrowing → Git Branches, Pull Requests and Conflict Resolution → Graph Traversal: BFS, DFS and Visited Sets → AWS IAM: Least Privilege and Secure Cloud Access → API Pagination, Error Contracts and Safe Retries →