Why This Matters
"It works on my machine" is the most infamous phrase in software engineering. You build an app on your laptop, hand it to a teammate, and it crashes because they have a different OS, different Python version, or a missing library. Containers solve this problem permanently.
A container packages your application with everything it needs to run: code, runtime, libraries, and system tools. Docker is the tool that builds and runs these containers. Once your app runs in a Docker container, it runs the same way everywhere — your laptop, your teammate's laptop, a CI server, or a cloud provider. Containers are the foundation of modern deployment. Kubernetes, cloud functions, and most CI/CD pipelines all build on top of containers.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
From code to container: write a Dockerfile, build an image, push to registry, run anywhere.
Code Example
// Dockerfile for a Node.js application
// ---- Dockerfile ----
// FROM node:20-alpine
// WORKDIR /app
// COPY package*.json ./
// RUN npm ci --production
// COPY . .
// EXPOSE 3000
// CMD ["node", "server.js"]
// docker-compose.yml - run multiple services
// version: "3.9"
// services:
// web:
// build: .
// ports:
// - "3000:3000"
// environment:
// - DATABASE_URL=postgres://db:5432/myapp
// db:
// image: postgres:16
// environment:
// - POSTGRES_PASSWORD=secret
// server.js - the app that runs inside the container
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({
status: "running",
hostname: require("os").hostname(),
uptime: process.uptime()
}));
});
server.listen(3000, () => {
console.log("Server running on port 3000");
});Interactive Experiment
Try these exercises:
- Install Docker Desktop and run
docker run hello-worldto verify it works. What does Docker download? - Create a simple
Dockerfilefor a Node.js or Python app. Build it withdocker build -t myapp .and run it withdocker run myapp. - Run
docker psto see running containers. Each container has its own ID, like a process has a PID. - Start a PostgreSQL database with
docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=secret postgres:16. Connect to it from your app. - Try changing your code and rebuilding. Notice how Docker caches unchanged layers and only rebuilds what changed.
Quick Quiz
Coding Challenge
Write a function called `createRegistry` that simulates a container registry. It should return an object with three methods: `push(name, tag, size)` stores an image (name:tag with size in MB), `pull(name, tag)` returns the image object or null if not found, and `list()` returns an array of all stored image strings in 'name:tag' format. If pushing an image that already exists (same name and tag), update its size.
Real-World Usage
Containers are the standard way to deploy software today:
- Docker Hub hosts millions of pre-built images: databases, web servers, programming languages.
docker run postgresgives you a database in seconds. - Kubernetes orchestrates thousands of containers across clusters of machines, handling scaling, networking, and self-healing.
- CI/CD pipelines use Docker to ensure builds are reproducible. GitHub Actions runs each job in a fresh container.
- Microservices architecture runs each service in its own container, enabling independent deployment and scaling.
- Docker Compose lets developers run entire multi-service applications locally with a single
docker compose upcommand.