What is Docker? Images, containers, Dockerfile, and Compose in practice
Learn what Docker is, the difference between an image, a container, and a registry, how to write a Dockerfile, use ports, volumes, and networks, run an app with Postgres using docker compose, and apply basic security.
Containers$ docker run -d -p 8080:80 nginx▍“It works on my machine.” Anyone who builds software has heard (or said) that line. The code runs on your laptop but breaks on the server because the Node version is different, a system library is missing, or an environment variable was left behind. Docker was created to solve exactly that: package the application together with everything it needs and run that package the same way anywhere.
That running package is a container: a process isolated from the rest of the system, with its own filesystem, network, and resource limits, but sharing the kernel (the core) of the host machine’s operating system. That’s why it’s much lighter than a virtual machine. The full comparison is in containers vs. virtual machines vs. serverless.
Image, container, and registry
Three concepts hold everything up:
| Concept | What it is | Analogy |
|---|---|---|
| Image | Read-only package with the app, dependencies, and configuration | The cake recipe (or a class) |
| Container | A running instance of an image | The baked cake (or an object) |
| Registry | Server that stores and distributes images | The recipe bookstore |
From the same recipe you can bake as many cakes as you want, all identical. The best-known registry is Docker Hub, which is the default when you don’t specify another server and hosts the official images (such as nginx, postgres, and node). There are also registries like GitHub Container Registry, Amazon ECR, and Harbor.
Under the hood: Docker Engine and Docker Desktop
Docker Engine is made of layers:
docker: the CLI, the program where you type commands.dockerd: the daemon (a service that keeps running in the background) that receives requests from the CLI, builds images, and manages networks and volumes.- containerd: manages the container lifecycle and image downloads.
- runc: the low-level runtime that actually creates the isolated process using Linux kernel features (namespaces and cgroups).
On Linux, you install the Engine directly. On macOS and Windows, Linux containers need a Linux kernel, and that’s where Docker Desktop comes in: a desktop app with a graphical interface that runs a lightweight Linux VM behind the scenes (on Windows, it can use WSL 2). Heads-up: check the Docker Desktop license terms for commercial use at larger companies. If that’s a problem, there are alternatives such as Podman and Kiri.
Your first Dockerfile
The Dockerfile is the recipe that produces the image. For a small Node.js API:
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
USER node
EXPOSE 3000
CMD ["node", "server.js"]
Copying package*.json before the rest of the code takes advantage of the layer cache: if only the code changed, npm ci doesn’t run again. Also create a .dockerignore so you don’t ship junk (or secrets) into the image:
node_modules
.git
.env
*.log
Everyday commands
docker build -t my-app:1.0 . # build the image
docker run -d --name app -p 8080:3000 my-app:1.0 # run in the background
docker ps # list running containers
docker logs -f app # follow the logs
docker exec -it app sh # open a shell inside the container
docker stop app && docker rm app # stop and remove
docker images # list local images
In -p 8080:3000, the first number is the port on your machine and the second is the port inside the container. Open http://localhost:8080.
Volumes and networks
A container’s filesystem is disposable: remove the container and the data is gone. To persist data, use volumes (managed by Docker) or bind mounts (a folder on your machine):
docker volume create data
docker run -d -v data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=change-me postgres:16
docker run --rm -v "$(pwd)":/src -w /src node:22-alpine npm test
For containers to talk to each other, create a network: on user-defined networks, one container finds another by name.
docker network create my-network
docker run -d --name db --network my-network -e POSTGRES_PASSWORD=change-me postgres:16
docker run -d --name app --network my-network -p 8080:3000 my-app:1.0
Docker Compose: multiple services in one file
Typing all of that by hand gets old fast. Docker Compose describes the whole application in YAML, and docker compose up starts everything, with a shared network already set up:
# compose.yaml
services:
app:
build: .
ports:
- "8080:3000"
environment:
DATABASE_URL: postgres://app:change-me@db:5432/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: change-me
POSTGRES_DB: app
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app"]
interval: 5s
retries: 5
volumes:
pgdata:
docker compose up -d # start the services
docker compose logs -f app
docker compose down # tear down (add -v to delete the volumes)
Notice that the app reaches the database through the host db, the service name.
Tags vs. digests
A tag (postgres:16) is a nickname and may start pointing to a different image when a fix is released. A digest (postgres@sha256:...) is the content’s fingerprint and never changes. Use readable tags day to day and digests when you need full reproducibility, such as in production. Avoid latest: it doesn’t mean “the newest”; it’s just the default tag used when none is given.
docker images --digests
Basic security
- Don’t run as root: use
USERin the Dockerfile, as in the example. - Prefer official and minimal images (
alpineorslimvariants): fewer packages, fewer vulnerabilities. - Scan your images with tools like Trivy or Docker Scout.
- Never put secrets in the image: anyone with access to it can extract them from the layers. Pass them at runtime or use BuildKit build secrets.
To go further, see multi-stage builds for smaller images.
What about Kubernetes?
Docker and Kubernetes don’t compete: Docker builds and runs containers on one machine; Kubernetes orchestrates containers across a cluster of many machines. Kubernetes no longer uses Docker Engine directly; it talks to runtimes that implement the CRI interface, such as containerd and CRI-O. But that changes nothing for you: images built with Docker follow the OCI standard and run just fine on the cluster. Learn the rest in how Kubernetes works.
Checklist
- Install Docker Engine (Linux) or Docker Desktop/an alternative (macOS and Windows)
- Write a Dockerfile with a non-root
USERand a.dockerignore - Build, run, and inspect a container with
build,run,logs, andexec - Persist data with a volume and connect containers through a network
- Start the app and Postgres together with
docker compose up -d - Pin versions with specific tags (or digests) instead of
latest - Scan your images and keep secrets out of them
Docker turns “it works on my machine” into “it works on any machine”: the recipe ships with the cake.