In this article, we explore the second rule of the 12-Factor App methodology: explicitly declaring and isolating dependencies. To illustrate this concept, we use the popular Python web framework, Flask. Before writing any application code, you must install Flask in your local development environment. For example:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
requirements.txt. This file should include both package names and specific version numbers. For example:
requirements.txt—including the specified version of Flask—is installed uniformly.
When developing multiple applications on a single machine, use isolated environments to avoid dependency conflicts. Virtual environments ensure each application manages its own version of every dependency.

requirements.txt with virtual environments guarantees that your explicit dependency packages are consistent across development, staging, and production.
In some cases, your application might depend on system-level tools (such as the curl command) that fall outside Python’s dependency management. In these instances, leveraging a platform like Docker can be highly effective. Docker containers offer a self-contained environment that ensures all necessary tools and configurations are present.
Below is an example Dockerfile that packages the Flask application along with its dependencies into a Docker container:
- Creates a Docker image based on the Python 3.10 Alpine base image.
- Sets the working directory to
/kodekloud-twelve-factor-app. - Copies the
requirements.txtfile into the working directory. - Installs the dependencies specified in
requirements.txt, without caching. - Copies the remaining application code into the image.
- Defines the command to run the application.