DEV Community

Python development environment in a Docker container

Mike on June 05, 2021

Purpose Create and setup a python development environment inside of docker in 5 minutes. (Also more or less a journal to myself of how t...
Collapse
 
clawsicus profile image
Chris Laws

In the first code example you create a virtual environment but do not activate it. This would result in you installing fastapi and hypercorn into the system python which is probably not what you wanted.

Collapse
 
mikecase profile image
Mike

Aye, you're right. Thanks for pointing that out.

Collapse
 
gwynevans profile image
Gwyn Evans • Edited

Don't you mean "source .venv/bin/activate", not "source .venv/bin/python"?

A couple of further minor points - your (optional?) "EXPOSE 8005" line should be "8000", shouldn't it? There's also a typo in the "docker-compose" line where you have "buid" rather than "build".

Collapse
 
lewisblakeney profile image
lewisblakeney

This is a game-changer! Using Docker for Python development is a brilliant idea. This setup will streamline workflows and ensure consistency across environments. A must-try for any Python development services!

Collapse
 
cjsmocjsmo profile image
Charlie J Smotherman

Ok, makes sense now. Thanks for the feedback :)

Collapse
 
mikecase profile image
Mike

Glad I could help. I will try to make the post a little more clear about that, I can see from the title now that it's kind of misleading.

Collapse
 
cjsmocjsmo profile image
Charlie J Smotherman

Why create a venv inside a container? I mean your python environment is already isolated by the container, why introduce a not needed layer of abstraction? Just curious :)

Collapse
 
mikecase profile image
Mike

You're not actually setting up a virtual environment inside the container. The container ultimately ends up being your end product. You can write your code and build your container and push it to the cloud/your server/github/dockerhub etc. It effectively sets up a CICD workflow. I will try to make this clearer in the post above but if you look at the Dockerfile you're not actually setting up a virtual environment inside the container. The only thing you do inside the container is install the required modules for your app to run and updating your codebase. Once your push that container to your server for example it can be live. No need to setup any kind of environments on your server it's all containerized.

Collapse
 
maanuanubhav999 profile image
anubhav_sharma

Thanks this was helpful.

Collapse
 
ninja_0b556e54be89f799ce9 profile image
ninja

Great walkthrough! This blog clearly explains how to set up a FastAPI development environment with Docker in just minutes. The step-by-step approach—covering virtual environments, Dockerfile, docker-compose, and bind mounts—makes it beginner-friendly yet practical. The helper scripts and troubleshooting tips are especially useful for smooth DevOps workflows.