Python Friday #101: Creating a Dev Container for Python

After creating dev containers for Ruby, Rails and .Net 6 it is time to take a look at Python and how we can put our development environment into a Docker container.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

Is a virtual environment not enough?

Virtual environments in Python are a great approach to create an isolated space to develop your application. You can have the right version of Python and your dependencies without any interference to your other projects.

However, this approach only covers Python and not your database or other services. For those dependencies a dev container is still the best approach and when you already have half of your application in a container, why not put the rest in it as well?

 

You probably don’t want to use Alpine

Alpine is a minimalistic Linux with an image size of just 5 MB. The small size is the reason so many tutorials take Alpine as a base layer. Unfortunately, Alpine uses the musl libc instead of glibc, what is a problem for the C extensions that may need to be recompiled at the build time of your container. How long that takes depends on your machine. Itamar Turner-Trauring explains this in more details in his PyCon talk Zero to production-ready: a best-practices process for Docker packaging.

You can take the official Python image for alpine (around 50 MB) or go directly to the slim edition based on Debian (around 120 MB) that uses the glibc and therefore works without any problems. I strongly suggest you spend 70 MB of your disk space once for all your Python containers instead of wasting hours with building C extensions.

 

Create a Dockerfile

We can create a Dockerfile similar to the one for Rails and install all the Linux packages we need for our application – and run pip at the build time of our container:

As explained above, I use the Debian-based slim container for Python 3.10 that you can find as an official Python image on hub.docker.com. If you need another version, you can take a more fitting tag.

 

Create a docker-compose.yaml file

I reuse the Postgres database container as a stand-in for any other dependency your application may have. You can take this docker-compose.yaml and add the dependencies you need as separate containers:

 

Start the Python container

We can start all our containers with this single command:

 

Make changes to the Python app

We can now connect Visual Studio Code to our dev container and develop new features for our Python application – or just check if everything works with Python 3.10.

 

Next

Creating a dev container for Python is not much different to creating one for Rails or .Net 6. They follow similar patterns and when you know how to create one it is a small step to create a dev container for another programming language. On Tuesday I summarise the dev container journey and send you off with some additional advice for your own dev environment.

2 thoughts on “Python Friday #101: Creating a Dev Container for Python”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.