Activate python virtualenv in Dockerfile

后端 未结 6 1835
忘了有多久
忘了有多久 2020-12-05 02:04

I have a Dockerfile where I try to activate python virtualenv after what, it should install all dependencies within this env. However, everything still gets installed global

6条回答
  •  温柔的废话
    2020-12-05 02:46

    There are perfectly valid reasons for using a virtualenv within a container.

    You don't necessarily need to activate the virtualenv to install software or use it. Try invoking the executables directly from the virtualenv's bin directory instead:

    FROM python:2.7
    
    RUN virtualenv /ve
    RUN /ve/bin/pip install somepackage
    
    CMD ["/ve/bin/python", "yourcode.py"]
    

    You may also just set the PATH environment variable so that all further Python commands will use the binaries within the virtualenv as described in https://pythonspeed.com/articles/activate-virtualenv-dockerfile/

    FROM python:2.7
    
    RUN virtualenv /ve
    ENV PATH="/ve/bin:$PATH"
    RUN pip install somepackage
    
    CMD ["python", "yourcode.py"]
    

提交回复
热议问题