Docker-based Python Development Workflow¶
A Docker-based Python Development Workflow utilizes containerization to establish a consistent and isolated environment for developing Python applications^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]. This approach typically involves defining the runtime environment in a Dockerfile, building a container image, and running the application within a container that mounts the local source code^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md].
Environment Setup¶
The workflow begins by defining the specific Python version and operating system in a Dockerfile^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md].
FROM python:3.9.6-alpine3.13 as dev
WORKDIR /work
This image is then built using the docker build command^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md].
docker build --target dev . -t python
Development Container¶
To facilitate an interactive development experience, the container is run with specific flags to enable code reloading and terminal access^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md].
- Volume Mount (
-v): Mounts the current working directory (${PWD}) to a directory inside the container (/work). This allows changes made on the host machine to be immediately reflected inside the container^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]. - Port Mapping (
-p): Exposes the application's port (e.g., 5000) to the host machine^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]. - Interactive Mode (
-it): Allocates a pseudo-TTY and keeps STDIN open, allowing the user to execute shell commands (likesh) within the container^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md].
Example command:
docker run -it -v ${PWD}:/work -p 5000:5000 --net redis python sh
Dependency Management¶
Dependencies are managed externally via a requirements.txt file, which is copied into the container and installed using pip^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md].
COPY ./requirements.txt /app/
RUN pip install -r /app/requirements.txt
Configuration¶
Sensitive configuration, such as database credentials or connection strings, is injected into the container at runtime using environment variables (-e) rather than being hardcoded into the image^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]. The Python application can access these via the os.environ module.
docker run -it -p 5000:5000 \
-e REDIS_SENTINELS="sentinel-0:5000,sentinel-1:5000,sentinel-2:5000" \
-e REDIS_MASTER_NAME="mymaster" \
customer-app
Production Deployment¶
For deployment, the Dockerfile can utilize multi-stage builds to create a lean production image^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]. The final stage copies only the installed dependencies and the source code, setting a default command (e.g., flask run) to execute the application^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md].
FROM dev as runtime
WORKDIR /app
COPY ./src/app.py /app/app.py
ENV FLASK_APP=app.py
CMD flask run -h 0.0.0 -p 5000
Related Concepts¶
- [[Virtual Environments]]
- [[Microservices]]
- CI/CD
Sources¶
^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]