Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Virtualenv

With containerized applications such as Docker, is it possible to use virtualenv such that the environment stays running from the build process, to when the image is finally run? It seems that creating a virtualenv via pip and running the environment never seems to work.

For example:

RUN pip3 install virtualenv
RUN virtualenv venv
RUN source venv/bin/activate

Would never seem to render a true virtual environment whereas following pip installs would be installed under the environment. The reason I need, or believe I need this setup, is that specific directories use dependencies that are different versions of neighboring directories: Folder B uses numpy version X, whereas Folder C uses numpy version Y.

With Docker, is there a tool or feature that I am forgetting that would allow me to use pip installs like a virtual environment, with different versions of the same dependency in different directories?

like image 858
Baily Avatar asked Sep 20 '26 15:09

Baily


1 Answers

Docker RUN is a build step. It creates a new layer upon previous, and essentially creates a new image. So the answer to your question is "No".

Consider using ENTRYPOINT and/or CMD, or compose a startup script for convenience.

Scripting your RUN might also suit your needs: RUN my_deploy_for_venv.sh will execute in one layer, so if you start venv in script, you shall have it during it's execution. You will have to start it again on container startup, though.

like image 196
Seer.The Avatar answered Sep 22 '26 03:09

Seer.The



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!