Containerizing a Backend Service
Why you package a backend service as a container image — the problem it solves and how a Dockerfile fits with twelve-factor config.
The Twelve-Factor App and Environment ConfigWeb Servers, Application Servers, and Reverse Proxies
Containerizing a Backend Service
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Containerizing a service means packaging your code together with its exact runtime, libraries, and system dependencies, so it runs the same way regardless of what machine it lands on.
- The problem this solves is dependency drift: a service that works on your laptop but breaks in staging because of a different OS, library version, or missing system package, even though the application code itself hasn't changed.
- A Dockerfile is a recipe — start from a base image, copy your code in, install dependencies, declare how to start the process — not a description of a whole virtual machine.
- Config still comes from environment variables injected at run time, never baked into the image, which is the same twelve-factor discipline applied to a packaging format instead of a codebase.
The problem before containers existed
Imagine handing a teammate your backend service and a page of setup instructions: install this exact version of your language runtime, these system libraries, this database client, set these environment variables, run it in this order. Now imagine that same handoff happening between your laptop and a staging server, and again between staging and production. Every one of those handoffs is a chance for something to be slightly different — a newer OS patch, a missing system package, a language runtime that’s a minor version off — and “slightly different” is exactly the gap where “works on my machine” and “breaks in production” live. None of that drift comes from your application code changing. It comes from the environment underneath it quietly not matching.
This is the actual problem containers solve. A container packages your code together with the specific runtime and system dependencies it needs, as a single artifact, so that artifact behaves identically no matter which machine runs it. The machine itself no longer has to be pre-configured to match your app — the app brings its own environment with it.
What a Dockerfile roughly does
You don’t need to know Docker’s full command surface to understand the idea. A Dockerfile is a short, ordered recipe, and nearly every one you’ll encounter follows the same shape:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "app:app", "--bind", "0.0.0.0:8000"]
FROM picks a base image — a starting point that already has an operating system layer and, usually, a language runtime pre-installed. You’re not building that layer yourself; you’re starting from one someone already built and maintains. COPY brings your application code and dependency manifest into the image. RUN executes a command at build time — almost always “install my dependencies” — baking the result into the image so it doesn’t need to happen again every time the container starts. CMD declares what process actually runs when a container starts from this image — in this case, the same application server (Gunicorn) you’d run without a container at all.
Building this file produces an image: a static, versioned artifact. Running that image produces a container: a live process executing in an isolated slice of the host machine’s resources. The same image can produce any number of containers, on any number of machines, and each one behaves identically — because the thing that used to vary between machines is now baked into the artifact itself.
How this connects to twelve-factor config
If a container bundles everything a service needs to run, it’s tempting to bundle configuration in there too — just hardcode the database URL into the image and be done with it. This is exactly the mistake twelve-factor config warns against, and containerizing a service doesn’t change that rule, it just moves where it applies.
The entire value of a container image is that it’s the same artifact everywhere — build once, deploy that exact image to dev, staging, and production. The moment you bake a staging database URL into the image, you need a different image per environment, and you’ve reintroduced the drift problem containers exist to eliminate, just one layer down. The fix is the same one twelve-factor config already prescribes: config comes from environment variables injected when the container starts, not from anything baked in when the image is built.
docker run -e DATABASE_URL=postgres://... -e STRIPE_API_KEY=sk_live_... myapp:latest
The image has no idea what environment it’s about to run in. It gets told, at startup, via the exact same environment-variable mechanism a non-containerized process would use. Containerizing a service is a packaging decision, not a configuration strategy — the configuration discipline underneath doesn’t change.
What this lesson is, and isn’t
This is a conceptual introduction to why backend services get packaged this way, not a Docker tutorial. Writing a production-quality Dockerfile well — minimizing image size, structuring layers so rebuilds are fast, multi-stage builds that strip out build tools from the final image, wiring multiple containers together with Compose, and eventually orchestrating many containers with something like Kubernetes — is real, substantial material that belongs to a dedicated container and DevOps track, not something to compress into one lesson here. If you want the equivalent idea applied to a different kind of artifact, containerizing models covers the same packaging logic for an ML model instead of a general backend service — the reasoning about reproducibility and environment parity carries over almost unchanged, even though what’s being packaged is different.
For now, the one idea worth keeping is this: a container is how you stop asking “does this machine have the right things installed” and start shipping an artifact that already answers that question for itself.
Check yourself
What specific problem does containerizing a backend service solve?
It solves dependency drift — the gap between "works on my machine" and "works in staging" or "works in production" caused by different OS versions, missing system libraries, or a different language runtime version on each machine. A container bundles the exact dependencies your code needs so that gap disappears.
At a conceptual level, what are the main pieces a Dockerfile puts together?
A starting point (the base image, which already has a language runtime and OS libraries installed), your application code copied in on top of it, a step that installs your application's own dependencies, and a declared command that starts your process when the container runs.
Why shouldn't you bake environment-specific config, like a database URL or an API key, into the image itself?
An image is meant to be the same artifact deployed to dev, staging, and production unchanged — that's the whole point of containerizing it. If config were baked in, you'd need a separate image per environment, which recreates the exact drift problem containers exist to prevent. Config still gets injected as environment variables when the container starts, exactly as twelve-factor config describes for a non-containerized process.
Does containerizing a service replace an application server and reverse proxy, or does it still need them?
It still needs them. A container just packages the same application server process (Gunicorn, uvicorn, Puma, Node) and its dependencies into a portable image — it doesn't change the fact that, in production, you generally still want a reverse proxy in front of it, whether that proxy lives in its own container, on the host, or in front of a whole cluster of containers.
This lesson calls itself a conceptual introduction — what's deliberately left out?
The actual mechanics of writing a production-grade Dockerfile, build caching and layering, Docker Compose for multi-container setups, and orchestrating many containers with something like Kubernetes. Those are real, substantial skills that belong to a dedicated container/DevOps track, not a single backend-roadmap lesson.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.