ramikrispin,
@ramikrispin@mstdn.social avatar

(1/3) Here is one of the most frequent questions I get on most of my Python 🐍+Docker 🐳 tutorials - why use a virtual environment inside a container?

The short answer is that you don't necessarily need a virtual environment (VE) to set a reproducible environment inside a container. Docker takes care of both the environment isolation and reproducibility.

I see VE as more of a practical method to organize your Python environment inside a container.

#python #docker #mlops #DataScience

ThreeSigma,
@ThreeSigma@mastodon.online avatar

@ramikrispin
I’ve been struggling with this. How do you deal with the issue if running executables under a process manager like pm2 or systemd or cron? I’ve found that both miniconda and venv handle it all terribly, requiring elaborate wrapper scripts or mocking of user environments. And for interactive work, I’m starting to find it tedious to set up my environment every time I open a new terminal tab. What am I missing??

ramikrispin,
@ramikrispin@mstdn.social avatar

@ThreeSigma I am not sure of your use case, but once you set the Docker image you can trigger it without resetting the env from scratch. Alternatively (again, depending on the context), you can set automation scripts to set it with bash or other tools such as Terraform, etc.

ThreeSigma,
@ThreeSigma@mastodon.online avatar

@ramikrispin Then I'm even more confused. If the docker container is configured to be in one of the virtual environments by default, isn't that just the same as having a static environment for the purposes of using it?

I'm guessing you spend a lot more time on infrastructure (or someone does) than I want to.

ramikrispin,
@ramikrispin@mstdn.social avatar

@ThreeSigma docker enables you to create an environment at the OS level, whereas venv enables you to create multiple Python environments. Here is an article I wrote a few months ago that summarizes the difference between venv and docker:

https://medium.com/p/4a62ed36900f

ramikrispin,
@ramikrispin@mstdn.social avatar

(2/3) Here is why you should consider using a VE when setting your Python inside a container:
➡️ It enables you to manage multiple environments inside the container instead of building multiple containers (e.g., dev, stg, prod) without the need to create multiple images 😎
➡️ It is a good practice and a more elegant way to set your requirements as opposed to dump it on your root ✨

ramikrispin,
@ramikrispin@mstdn.social avatar

(3/3) ➡️ In some cases, your base image might come with multiple Python versions. It is very convenient to name your environment when working with Jupyter or similar tools to identify the kernel
➡️ It takes between one and three lines of code to set a VE (depending on what method you are using); don't be lazy 😜

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • thenastyranch
  • DreamBathrooms
  • tacticalgear
  • magazineikmin
  • khanakhh
  • everett
  • Youngstown
  • mdbf
  • slotface
  • rosin
  • ethstaker
  • InstantRegret
  • kavyap
  • ngwrru68w68
  • megavids
  • cisconetworking
  • cubers
  • osvaldo12
  • modclub
  • GTA5RPClips
  • tester
  • Durango
  • provamag3
  • anitta
  • Leos
  • normalnudes
  • JUstTest
  • lostlight
  • All magazines