Using 'pyenv activate' in a Dockerfile
Asked Answered
T

1

8

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 ?

Tartary answered 30/10, 2016 at 14:17 Comment(5)
Not exactly a solution, but I use the “real” executable path in this situation. Instead of relying on pyenv activate, just replace pip with $PYENV_ROOT/versions/foo/bin/pip.Ullman
Personally, I don't see the need for virtualenvs within a container, but others might disagreeIncontrollable
people that might want to install any specific version of python on a specific dockerfile will disagreeSkilling
@Tartary did you ever solve/work around this?Jacobine
I feel embarrassed to admit it, but I had the same issue, and in my case it was due to the fact that I was copy-pasting from two different stack overflow instructions, setting PYENV_ROOT and the path variables to some path (/opt/pyenv/) and the .bashrc stuff to another (~/.pyenv).Gruff
S
1

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"
Secluded answered 29/10, 2019 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.