简体   繁体   中英

Using 'pyenv activate' in a Dockerfile

I'm trying to install and setup a pyenv within a Dockerfile ( FROM nvidia/cuda:8.0-cudnn5-devel-ubuntu16.04 )

Here is the second half of the Dockerfile ( the first half is just installing dependencies ) :

RUN useradd -ms /bin/bash user && echo "user:resu" | chpasswd && adduser user sudo
USER user
WORKDIR /home/user

# install pyenv
RUN git clone git://github.com/yyuu/pyenv.git .pyenv
ENV HOME  /home/user
ENV PYENV_ROOT $HOME/.pyenv
ENV PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH
RUN echo 'export PYENV_ROOT="$HOME/.pyenv"' >> .bashrc
RUN echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> .bashrc
RUN echo 'eval "$(pyenv init -)"' >> .bashrc
RUN pyenv install 2.7.10

# install pyenv-virtualenv
RUN git clone https://github.com/yyuu/pyenv-virtualenv.git .pyenv/plugins/pyenv-virtualenv
RUN echo 'eval "$(pyenv virtualenv-init -)"' >> .bashrc

# setup virtualenv
RUN pyenv virtualenv 2.7.10 foo
RUN /bin/bash -c '      source .bashrc && \
                        pyenv activate foo && \
                        pip install numpy && \
                        pip install nltk'

The Docker installation fails on the last line with :

Step 20 : RUN /bin/bash -c '    source .bashrc &&                       pyenv activate foo &&                      pip install numpy &&                    pip install nltk'
 ---> Running in 672826e55a40

Failed to activate virtualenv.

Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.

What is the problem here ? Is there a best-practice for setting up pyenvs with Docker ?

Most likely your source .bashrc is ignored (and thus the default pyenv setup does not proceed) because the bashrc is often configured to abort if not in the interactive mode .

As a workaround, add -i interactive switch, or use another hack from the linked discussion. For this example, this would be:

bash -i -c "source ~/.bashrc && pyenv activate foo"

Alternatively, you can run the relevant commands setting up the pyenv (you will find them in your .bashrc ) before the activation step. For me this would be:

bash -c "\
export PATH="/home/krassowski/.pyenv/bin:$PATH" &&\
eval "$(pyenv init -)" && \
eval "$(pyenv virtualenv-init -)" && \
pyenv activate foo"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM