Pack an app with everything it needs into one light, portable box that runs the same everywhere.
A container packs an app together with everything it needs to run (libraries, tools, settings) into one light, portable box. Docker is the best-known tool for building and running them.
Before containers, you installed an app by hand on a server: the right language version, the right libraries, the right settings. Two apps on one machine could need different versions of the same library and fight each other. And the test server never quite matched production.
A container solves this by bundling the app with its dependencies. Under the hood, Linux features isolate it: namespaces give each container its own view of processes, network and files, and control groups (cgroups) cap how much CPU and memory it may use. It looks like a small separate computer, but it is only a well-fenced process.
# Small base image, with a pinned version FROM node:20-slim WORKDIR /app # Copy the dependency list first, so this layer is cached COPY package.json package-lock.json ./ RUN npm ci --omit=dev # Copy the source last, because it changes most often COPY . . # Do not run as root USER node EXPOSE 3000 CMD ["node", "server.js"]
Each instruction makes a layer. Docker reuses a cached layer if that line and every line above it are unchanged. That is why the dependency list is copied and installed before the source code: dependencies change rarely, and the source changes all the time.
| Container | Virtual machine | |
|---|---|---|
| What it runs | An app and its libraries, sharing the host kernel | A full guest operating system |
| Start time | Moments (illustrative: seconds) | Slower (illustrative: tens of seconds to minutes) |
| Size and memory | Small, light | Large, heavy |
| Isolation | Good, but the kernel is shared | Stronger, with a separate kernel |
| Different OS | Same kernel family as the host | Any OS the hypervisor supports |
| Typical use | Apps, services, jobs | Strong separation, other operating systems |
| Term | Meaning in easy words |
|---|---|
| Container | An isolated process that carries its own libraries and shares the host kernel |
| Image | A read-only package of layers from which containers are started |
| Dockerfile | A text recipe that says how to build an image |
| Layer | One step of an image, which can be cached and shared |
| Registry | A store for images, such as Docker Hub or a private registry |
| Tag | A human-friendly name for an image version, such as 1.4 |
| Digest | A hash that names exactly one image |
| Volume | Storage that lives outside the container and survives its removal |
| Namespace | A Linux feature that gives a process its own view of things like processes and network |
| cgroup | A Linux feature that limits the CPU and memory a process may use |
| Orchestrator | A system such as Kubernetes that runs and manages many containers |
| Choice | We gain | We lose |
|---|---|---|
| Containers instead of VMs | Speed and density | Some isolation strength |
| Small base image | Fast pulls and a smaller attack surface | Fewer built-in tools for debugging |
| Many tiny containers | Independent scaling and deploys | More moving parts to run and watch |
| Pin exact versions | Repeatable builds | You must update them on purpose |
| Use the latest tag | No manual updates | Surprise changes and hard rollbacks |
| Volume for data | Data survives restarts | The container is tied to that storage |