DevOps

Docker for beginners: images, containers and Compose

What actually happens between docker build and a running container, and when Compose saves the day.

Docker for beginners: images, containers and Compose

Image and container: what is the difference

An image is an immutable template: application code, runtime, libraries and configuration. A container is a running process created from that image with its own isolated filesystem and network namespace.

This separation is the main benefit. You build once, then run the same image locally, in CI and in production. Configuration and secrets are supplied at runtime, not baked into the artifact.

  • Dockerfile describes how to build the image.
  • The image is stored in a registry and tagged by version.
  • The container is disposable: stop it, remove it, start a new one.

A Dockerfile you can read

A minimal Node.js image starts from an official runtime, copies dependency manifests, installs packages, copies the source and defines the start command. Order matters: dependency installation is cached separately from source changes.

Use a multi-stage build for production: one stage compiles assets, another copies only the runtime files. This keeps the final image small and reduces the attack surface.

When Compose is enough

Compose describes several services in one YAML file: an application, a database, a reverse proxy. It is perfect for local development and small deployments where Kubernetes would be overkill.

Persist database data in named volumes, pass secrets through environment variables or a secret manager, and avoid running everything as root. These small habits prevent most painful incidents.

FAQ

How is a container different from a virtual machine?
A container shares the host kernel and isolates the process, while a VM runs a full guest operating system. Containers start faster and use fewer resources, but provide a different isolation boundary.
Where should container data be stored?
Use named volumes or an external database service. Data inside the container filesystem disappears when the container is recreated.