Activate python virtualenv in Dockerfile

后端 未结 6 1832
忘了有多久
忘了有多久 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:31

    Setting this variables

    ENV VIRTUAL_ENV /env
    ENV PATH /env/bin:$PATH
    

    is not exactly the same as just running

    RUN . env/bin/activate
    

    because activation inside single RUN will not affect any lines below that RUN in Dockerfile. But setting environment variables through ENV will activate your virtual environment for all RUN commands.

    Look at this example:

    RUN virtualenv env                       # setup env
    RUN which python                         # -> /usr/bin/python
    RUN . /env/bin/activate && which python  # -> /env/bin/python
    RUN which python                         # -> /usr/bin/python
    

    So if you really need to activate virtualenv for the whole Dockerfile you need to do something like this:

    RUN virtualenv env
    ENV VIRTUAL_ENV /env                     # activating environment
    ENV PATH /env/bin:$PATH                  # activating environment
    RUN which python                         # -> /env/bin/python
    

提交回复
热议问题