25
submitted 3 months ago* (last edited 3 months ago) by alexdeathway@programming.dev to c/python@programming.dev

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?

you are viewing a single comment's thread
view the rest of the comments
[-] bjorney@lemmy.ca 10 points 3 months ago* (last edited 3 months ago)

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
[-] GBU_28@lemm.ee 5 points 3 months ago

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

[-] dwt@feddit.org 3 points 3 months ago

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

[-] uthredii@programming.dev 2 points 3 months ago

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.

[-] sweng@programming.dev 3 points 3 months ago

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).

[-] uthredii@programming.dev 1 points 3 months ago

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.

[-] sweng@programming.dev 1 points 3 months ago

Sure If that happens. But it may also not. Which is actually usually the case. Sure, it's not 100% safe, but it is safer.

[-] naught@sh.itjust.works -2 points 3 months ago

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)

this post was submitted on 04 Sep 2024
25 points (90.3% liked)

Python

6478 readers
1 users here now

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

📅 Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS