VII. Port binding

Export services via port binding

“The twelve-factor app is completely self-contained.” – 12factor.net

When to use

Always.

Why it matters

  • Self-contained: The app includes its own webserver. It doesn’t rely on Apache/Nginx being pre-installed and configured on the host.
  • Composability: App A can talk to App B via localhost:5000.

Signs of Violation

  • Code is just a set of .php files that requires an external Apache/PHP-FPM setup to even run.
  • Code is a .war file that needs a Tomcat container.

Explanation

Problem

Dependency on a complex “Application Server” container makes dev/prod parity hard and deployment complex.

Solution

The app should start and listen on a PORT. npm start -> Listening on 3000.

Real world analogy

A food truck. It accepts customers (Traffic) directly through its own window (Port). It doesn’t need to be installed inside a mall food court to operate; it brings its own kitchen.

Pros and Cons

Pros Cons
  • Run anywhere
  • Simple contract
  • Might still need a reverse proxy (Nginx) for SSL/Load Balancing at the edge
  • Comparison

    • CGI/PHP-FPM: Traditional PHP violates this (relying on FPM). Modern PHP (Swoole, RoadRunner, or even php -S) adheres to it.

    Code example

    Typescript

    Bad (Violation)

    Good (Adherence)

    PHP

    Bad (Violation)

    Good (Adherence)