12-Factor App: Codebase, Dependencies, Config, Backing Services, and Releases
Cloud-native context and the 12-Factor Manifesto
Cloud-native applications are designed to exploit cloud computing’s elasticity, scalability, and automation. In practice, this usually means running containerized microservices on an orchestrator such as Kubernetes, which abstracts raw compute, storage, and networking primitives and handles resource allocation automatically.
The 12-Factor App Manifesto—originated by Heroku developers in 2011—codifies twelve best practices for building “software-as-a-service” microservices that remain portable, scalable, and maintainable. Its first five principles govern how an application is organized, packaged, configured, wired to external services, and released before handling production traffic.
Factor I: Single Codebase
- Problem solved: When application versions live across separate directories or uncoordinated repositories, teams lose track of what is running in production and cannot reliably reproduce or roll back a release.
- Mechanism: Every file belonging to one application lives in one version-controlled repository (e.g., a single Git repo). Crucially, this includes not only application source code, but also template files (
Dockerfile), Infrastructure as Code manifests (Kubernetes YAMLs), and documentation. - Consequence: A single source of truth makes builds repeatable, simplifies switching between versions, and acts as a prerequisite enabler for Continuous Integration / Continuous Delivery (CI/CD) pipelines.
Factor II: Explicitly Declare and Isolate Dependencies
- Problem solved: An application that assumes certain libraries or OS tools are pre-installed on the host machine breaks unpredictably when scheduled onto another node.
- Analogy: A food truck carrying its own generator, water tank, and stove is self-contained and can cook in any parking lot. A kitchen that assumes every lot already provides 240V power and a gas line fails whenever it moves. (Limit of the analogy: external shared databases and storage are not carried inside the truck—those are separated by Factor IV.)
- Mechanism (Dockerfiles and multi-stage builds): Containers package the application and all required libraries and OS executables into one self-contained image (see Dockerfiles Builds and Multistage Images). The lecture illustrates this with a multi-stage Go
Dockerfile:- Builder stage (
FROM golang:1.19 as builder): SetsWORKDIR /tmp/builder, copies explicit dependency manifests (go.modandgo.sum), runsRUN go mod download, copies source code, and compiles./cmd/app. - Runtime stage (
FROM scratch): Starts from an empty image, copies only the compiled binary (COPY --from=builder /tmp/builder/app /usr/bin/app), and setsENTRYPOINT ["/usr/bin/app"].
- Builder stage (
- Tradeoff: Maintaining explicit manifests and build recipes adds upfront discipline, in exchange for minimal, deterministic, host-independent images.
- Common confusion: Code dependencies (libraries, binaries bundled inside the image) belong to Factor II, whereas external backing services (databases, message brokers) belong to Factor IV.
Factor III: Configuration as Environment Variables
- Problem solved: Hardcoding environment-specific values (database URLs, passwords, greetings) inside source code or container images forces a rebuild for each environment and risks leaking credentials in version control.
- Mechanism: The application must be environment-agnostic—the exact same container image runs unmodified in development, staging, and production, with configuration injected from outside at runtime.
Kubernetes provides four main mechanisms for external configuration (see also Kubernetes Pods Sidecars and Workload Controllers):
| Mechanism | What it supplies | How it is delivered | Source example / use case |
|---|---|---|---|
Environment variables (env) | Static key-value settings in a Pod spec | Process environment variables | DEMO_GREETING / DEMO_FAREWELL on envar-demo-container |
ConfigMap | Non-sensitive configuration data or files | Env vars or mounted volume files | Application URLs, feature flags, config files |
Secret | Sensitive credentials, tokens, TLS certs | Env vars or mounted volume files (RBAC-restricted) | Database passwords, API keys |
| Downward API | Metadata about the Pod itself or surrounding cluster | Env vars or downwardAPI volume via fieldRef | Injecting metadata.labels and metadata.annotations to /etc/podinfo |
In the lecture’s Downward API example (kubernetes-downwardapi-volume-example), a busybox Pod mounts a downwardAPI volume named podinfo at /etc/podinfo with fieldRef entries pointing to fieldPath: metadata.labels (zone: us-east-coast, cluster: test-cluster1) and fieldPath: metadata.annotations (build: two), letting the container inspect cluster metadata without calling the Kubernetes API directly.
Common confusion: Use a
ConfigMapfor custom settings your team invents (such as a greeting string or feature toggle), and the Downward API for metadata Kubernetes already tracks about the Pod or cluster (such asmetadata.labelsormetadata.annotations).
Factor IV: Backing Services as Attached Resources
- Problem solved: Tightly coupling an app to a local database or storage daemon running inside the same host prevents independent scaling and makes swapping providers painful.
- Analogy: Household appliances plug into a standard wall socket without caring whether power comes from solar panels, a hydro dam, or a backup generator.
- Mechanism: Any non-core service the application consumes—databases, external object storage, caches, external APIs—lives outside the application and is accessed as a service over the network (e.g., HTTP or gRPC), using endpoints and credentials supplied via Factor III configuration.
- Kubernetes enforcement:
- The Kubernetes
Serviceabstraction (see Kubernetes Services Ingress and Reverse Proxy) hides whether the real endpoint is a cluster Pod or an external endpoint. - Kubernetes also supports cloud-agnostic APIs to instantiate and provision services (such as a DBMS) without locking application code to a specific cloud vendor.
- The Kubernetes
Factor V: Build, Release, Run
(Source note: Printed slide 11 titles this section “Build, Run, Release”, though the lifecycle order described is Build -> Release -> Run.)
- Problem solved: Editing or patching files directly on a running production machine destroys reproducibility, makes version identification impossible, and breaks clean rollbacks.
- Mechanism: Strictly separate the three lifecycle stages:
- Build: Convert code and declared dependencies (Factors I–II) into an immutable, version-tagged container image stored in a container registry (e.g.,
hello-app:2.0). - Release: Combine the built image with environment-specific configuration (Factor III) and cluster manifests. In Kubernetes, a release on a specific environment consists of four elements: Application Code, Container Template (
Dockerfile), Kubernetes infrastructure objects (Deployment,Service), and Environment Configuration (ConfigMaps,Secrets). - Run: Execute the identified release in the target cluster (Kubernetes schedules and runs the Pods).
- Build: Convert code and declared dependencies (Factors I–II) into an immutable, version-tagged container image stored in a container registry (e.g.,
- Package and release managers (Kustomize vs. Helm): Adapting base Kubernetes manifests manually for each environment is error-prone. Kustomize customizes base YAML manifests per environment, while Helm acts as a Kubernetes package manager—comparable to
aptoryumin Linux. Helm bundles related Kubernetes object templates into a Chart and injects environment-specific inventory values (such as the exposed application URL) to deploy a versioned Helm release.
Retrieval
- A Python microservice uses the
requestslibrary to call an external PostgreSQL database. According to Factors II, III, and IV, where should (a)requests, (b) PostgreSQL itself, and (c) the database password live?
(a)requestsis an internal code dependency bundled inside the container image (Factor II); (b) PostgreSQL runs outside the app as an attached backing service accessed over the network (Factor IV); (c) the password is injected externally via a KubernetesSecret(Factor III) so the image remains environment-agnostic. - A Pod needs to know both the
zonelabel assigned to it by the cluster and a team-defined welcome banner. Which Kubernetes mechanism fits each, and what breaks if either is baked into theDockerfile?
Thezonelabel (metadata.labels) comes from the Downward API, whereas the team-defined welcome banner belongs in aConfigMap(orenv). Baking either into theDockerfilebreaks environment agnosticism, forcing separate image rebuilds for every cluster zone or environment. - What four components define a Kubernetes release under Factor V, and why do teams use Helm charts?
A Kubernetes release combines application code, theDockerfile, Kubernetes objects (Deployment,Service), and environment configuration (ConfigMaps,Secrets). Helm charts package those related templates like an OS package (apt/yum) and inject environment-specific values to produce repeatable, versioned releases.
Source: Lecture 12 - Cloud Native Applications and the 12-Factor App, PDF pages 2–12 (printed slides 3–13).