Containers
A container packages an application together with its dependencies (runtime, libraries and configuration) into a single, immutable image that runs the same way on a laptop, in a test environment and in production.
As environments became rules-based and API-driven, it became possible to wrap up an application and its environment into a single package and simply move it from place to place. This containerisation means we can progress environments, not just code, through development, test and production as part of an iterative build.
How containers work
Unlike a virtual machine, a container doesn’t include a whole operating system. Containers share the host’s kernel and use operating system features to isolate processes, file systems and networking from each other. That makes them:
- lightweight: an image can be a few megabytes, and a container starts in seconds or less
- consistent: “it works on my machine” becomes “it works in this image”, and the same image is promoted through every environment
- immutable: you don’t patch a running container. You build a new image and replace it, which fits naturally with immutable infrastructure
The key concepts are:
- Image: a read-only template, usually defined in a
Dockerfile(orContainerfile) and built in CI. Images follow the Open Container Initiative (OCI) standard, so they’re portable between tools and clouds. - Registry: where images are stored and versioned, for example Docker Hub, Amazon ECR, Azure Container Registry, Google Artifact Registry or GitHub Container Registry.
- Container: a running instance of an image.
Orchestration
Running one container is easy. Running hundreds of them reliably, with scaling, networking, rolling deployments, health checks, secrets and service discovery, needs an orchestrator:
- Kubernetes is the de facto open standard, available as a managed service on every major cloud (Amazon EKS, Azure Kubernetes Service, Google Kubernetes Engine). It’s extremely powerful and extensible, but it’s a platform in its own right, with a significant learning and operational cost.
- Cloud-native orchestrators such as Amazon ECS are simpler and tightly integrated with their provider’s services.
- Serverless containers such as AWS Fargate, Azure Container Apps and Google Cloud Run let you run containers without managing any servers or clusters at all. You provide an image and the platform runs and scales it, often down to zero.
Following the mental model, start with serverless containers and only take on a full Kubernetes platform when you genuinely need what it offers.
When to use containers
Containers are a good fit when:
- you have existing applications or long-running services that you want to make portable and consistent across environments
- you need a runtime, library or workload shape (long-running, stateful-ish, high and steady throughput) that doesn’t suit serverless functions
- you want a single packaging standard across many teams and languages
They’re less of a fit when a fully managed or serverless service already solves the problem. A container you build is still software you have to patch: base images need regular rebuilding and scanning for vulnerabilities, and you’re responsible for everything inside the image.
Good practice
- Keep images small, using minimal or “distroless” base images, and rebuild them regularly to pick up security fixes.
- Scan images for vulnerabilities in CI and sign them so you know what you’re running.
- Run as a non-root user and give containers only the permissions they need.
- Keep containers stateless. Put state in managed databases, caches or object storage.
- Configure through environment variables and secrets managers, never by baking secrets into images.
- Log to standard output and let the platform collect logs and metrics.