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:
    1. Builder stage (FROM golang:1.19 as builder): Sets WORKDIR /tmp/builder, copies explicit dependency manifests (go.mod and go.sum), runs RUN go mod download, copies source code, and compiles ./cmd/app.
    2. Runtime stage (FROM scratch): Starts from an empty image, copies only the compiled binary (COPY --from=builder /tmp/builder/app /usr/bin/app), and sets ENTRYPOINT ["/usr/bin/app"].
  • 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):

MechanismWhat it suppliesHow it is deliveredSource example / use case
Environment variables (env)Static key-value settings in a Pod specProcess environment variablesDEMO_GREETING / DEMO_FAREWELL on envar-demo-container
ConfigMapNon-sensitive configuration data or filesEnv vars or mounted volume filesApplication URLs, feature flags, config files
SecretSensitive credentials, tokens, TLS certsEnv vars or mounted volume files (RBAC-restricted)Database passwords, API keys
Downward APIMetadata about the Pod itself or surrounding clusterEnv vars or downwardAPI volume via fieldRefInjecting 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 ConfigMap for 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 as metadata.labels or metadata.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 Service abstraction (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.

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:
    1. 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).
    2. 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).
    3. Run: Execute the identified release in the target cluster (Kubernetes schedules and runs the Pods).
  • 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 apt or yum in 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

  1. A Python microservice uses the requests library 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) requests is 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 Kubernetes Secret (Factor III) so the image remains environment-agnostic.
  2. A Pod needs to know both the zone label 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 the Dockerfile?
    The zone label (metadata.labels) comes from the Downward API, whereas the team-defined welcome banner belongs in a ConfigMap (or env). Baking either into the Dockerfile breaks environment agnosticism, forcing separate image rebuilds for every cluster zone or environment.
  3. What four components define a Kubernetes release under Factor V, and why do teams use Helm charts?
    A Kubernetes release combines application code, the Dockerfile, 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).