I recently join in current project and find steps in ReadMe like this (and I can't contact with man who create it):
# install pyenv git clone git://github.com/pyenv/pyenv.git ~/.pyenv ... pyenv install 3.7.9 pyenv global 3.7.9 # install venv pip install virtualenv # create virtual environment source .venv/bin/activate # install dependencies pip install pipenv pipenv install --dev ... So my questions are:
- what is reason/profit of using virtual environment inside virtual environment?
- what is reason/profit of using pyenv or venv if we running application inside python container? Isn't better idea to install all libraries using docker's system pip/python? Docker container is already abstract layer (virtual environment).
pyenv already creating user depending environment that can be easily removed/changed/reseted without influence on system python libraries
In other way environment created with virtualenv still depending on system libraries, so It can't be moved easily between servers.
Maybe here is some benefits or good practices of using venv when service deploying?
Even localstack is using virualenv inside docker. Isn't docker isolation level is not enough?
Update 2022/06/02
According this answer looks like virtualenv may be used to keep size of resulted image smaller.
I checked two patterns:
- staged build with
virtualenv
FROM python:3-alpine as compiler ENV PYTHONUNBUFFERED 1 WORKDIR /app/ RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" COPY ./r.txt /app/r.txt RUN pip install -Ur r.txt FROM python:3-alpine as runner WORKDIR /app/ COPY --from=compiler /opt/venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" CMD ["sleep", "999" ] and
- unstaged build without
virtualenv
FROM python:3-alpine as runner WORKDIR /app/ COPY ./r.txt /app/r.txt RUN python -m venv /opt/venv RUN pip install -Ur r.txt && pip cache purge CMD ["sleep", "999" ] where r.txt is:
django django-rest-framework flask fastapi Result is:
$ docker images | grep stage unstaged_python latest 08460a18018c ... 160MB staged_python latest dd606b218724 ... 151MB Conclusion:
venv may be used to save image total size, however size difference is not so big. Also unstaged image may be little bit more cleaned after pip installation to reduce total size. In other words venv usage may reasonable when we have many heavy build operations using compile tools that needed only when build (compile) timing and may be removed when image is ready.