- Have one codebase.
- Explicitly declare and isolate dependencies.
- Store configuration in the environment.
- Treat backing services as attached resources.
- Strictly separate build and run stages.
- Execute the app as one or more stateless processes.
- Export services via port binding.
- Scale out via the process model.
- Maximize robustness with fast startup and graceful shutdown.
- Keep development, staging, and production as similar as possible.
- Treat logs as event streams.
- Run admin or management tasks as one-off processes.
A Simple Flask Example
We begin with a basic Flask application that displays a simple message when accessed via a browser. The entry point for our Flask app is theapp.py file, which is responsible for handling all incoming and outgoing requests.
Below is the code for our simple Flask app:
At this stage, the code is stored only on your local machine.
The First Factor: One Codebase
The first principle of the 12-Factor App methodology insists on maintaining a single codebase per application. This approach is essential as your user base expands and new features are added, especially when multiple developers are involved. Each developer works on their own local environment while committing changes to a central repository. Without a proper version control system, concurrent modifications by different developers could lead to conflicts. This is where Git becomes invaluable. Git enables concurrent contributions to the same codebase by facilitating operations like pulling the latest changes with thegit pull command, making local updates, and pushing new commits using git push.
The central repository is typically hosted on a cloud platform. For example, GitHub is a popular platform for hosting Git repositories, while GitLab and Bitbucket offer similar solutions.

Managing Multiple Applications
Consider starting with an initial web application. Over time, you might expand your system by adding services such as order processing or delivery functionalities. In the past, it was common to maintain a single codebase for all related applications. However, once multiple services are deployed, the architecture becomes distributed, and sharing one codebase across multiple applications violates the 12-Factor App principles. Each application should reside in its own codebase.
