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 ?
pyenv will help you to streamline the creation, management and activating virtual environments. In the old days, before the virtualenv became popular, I would keep a single global workspace for all my Python projects.
In Conclusion. While Docker provides an isolated environment for your Python application, you're better off by using virtualenv (or your tool of choice) nevertheless. It can help you to maintain control over your Python environment & dependencies.
Poetry is a package manager for python. It needs python to install, but once it is installed it will use whatever python is in your path (i.e., whatever you set with pyenv ). If the system python is new enough, it is fine to use the system python to install poetry.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With