I read some articles about using a virtual environment in Docker. Their argument are that the purpose of virtualization in Docker is to introduce isolation and limit conflicts with system packages etc.

However, aren’t Docker and Python-based images (e.g., python:*) already doing the same thing?

Can someone eli5 this whole thing?

10 points
*

It’s not necessary but there is no reason not to.

Pros:

  • production and development programs are more similar
  • upgrading your base image won’t affect your python packages
  • you can use multi stage builds to create drastically smaller final images

Cons:

  • you have to type venv/bin/python3 instead of just python3 in the run line of your dockerfile
permalink
report
reply
5 points

Hah my base python has never seen a command.

Biggest reason for me is that local dev happens in a venv and translating to a container is 100% 1:1 then

permalink
report
parent
reply
3 points

It’s easy to set the path to include the venv in the Dockerfile, that way you never have to activate, either in the run line, nor if you exec into it. Also this makes all your custom entry points super easy to use. Bonus, it’s super easy to use uv to get super fast image builds like that. See this example https://gist.github.com/dwt/6c38a3462487c0a6f71d93a4127d6c73

permalink
report
parent
reply
2 points

upgrading your base image won’t affect your python packages

Surely if upgrading python will affect your global python packages it will also affect your venv python packages?

you can use multi stage builds to create drastically smaller final images

This can also be done without using venv’s, you just need to copy them to the location where global packages are installed.

permalink
report
parent
reply
3 points

Upgrading the base image does not imply updating your python, and even updating your python does not imply updating your python packages (except for the standard libraries, of course).

permalink
report
parent
reply
1 point

Sure, but in the case where you upgrade python and it affects python packages it would affect global packages and a venv in the same way.

permalink
report
parent
reply
-3 points

If you’re on an apple silicon mac, docker performance can be atrocious if you are emulating. It can also be inconvenient to work with Docker volumes and networks. Python already has pyenv and tools like poetry and rye. Unless there’s a need for Docker, I personally would generally avoid it (tho I do almost all my deployments via docker containers)

permalink
report
parent
reply
6 points
permalink
report
reply
-2 points

not exactly.

permalink
report
parent
reply
5 points

Does Hynek’s article convince you?

permalink
report
parent
reply
6 points

It’s a bit unclear to me what you refer to with “their argument”. What argument exactly?

permalink
report
reply
1 point

need for isolation inside container even with python image.

permalink
report
parent
reply
5 points

Could you share the article?

permalink
report
reply
3 points

I can think of only two reasons to have a venv inside a container:

  • If you’re running third-party services inside a container, pinned to different Python versions.

  • If you do local development without docker and scripts that have to activate the venv from inside the script. If you move the scripts inside the container, now you don’t have a venv. But then it’s easy to just check an environment variable and skip, if inside Docker.

For most applications, it seems like an unnecessary extra step.

permalink
report
reply
2 points

If you do multi stage builds (example here) it is slightly easier to use venvs.

If you use the global environment you need to hardcode the path to global packages. This path can change when base images are upgraded.

permalink
report
parent
reply
1 point

But then it’s easy to just check an environment variable and skip, if inside Docker.

How is forcing your script to be Docker-aware simpler than just always creating a venv?

permalink
report
parent
reply
3 points

One Docker env variable and one line of code. Not a heavy lift, really. And next time I shell into the container I don’t need to remind everyone to activate the venv.

Creating a venv in Docker just for the hell of it is like creating a symlink to something that never changes or moves.

permalink
report
parent
reply
-1 points

How can you be sure it’s one line of code? What if there are several codepaths, and venvs are activated in different places? And in any case, even if there is only one conditional needed, that is still one branch more than necessary to test.

Your symlink example does not make sense. There is someting that is changing. In fact, it may even be the opposite: if you need to use file A in s container, and file B otherwise, it may make perfect sense to symlink the correct file to C, so thst your code does not need to care about it.

permalink
report
parent
reply

Python

!python@programming.dev

Create post

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events
October 2023
November 2023
Past

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
  • Pythörhead: a Python library for interacting with Lemmy
  • Plemmy: a Python package for accessing the Lemmy API
  • pylemmy pylemmy enables simple access to Lemmy’s API with Python
  • mastodon.py, a Python wrapper for the Mastodon API
Feeds

Community stats

  • 528

    Monthly active users

  • 396

    Posts

  • 1.9K

    Comments