在Dockerfile

时间:2016-10-30 14:17:18

标签: python ubuntu docker virtualenv pyenv

我正在尝试在Dockerfile中安装和设置pyenv(FROM nvidia/cuda:8.0-cudnn5-devel-ubuntu16.04

这是Dockerfile的后半部分(前半部分只是安装依赖项):

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'

Docker安装在最后一行失败:

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.

这是什么问题?是否有使用Docker设置pyenv的最佳实践?

1 个答案:

答案 0 :(得分:0)

您的source .bashrc很可能会被忽略(因此默认pyenv设置不会继续进行),因为bashrc通常配置为abort if not in the interactive mode

作为解决方法,添加-i交互式开关,或使用链接讨论中的其他技巧。对于此示例,它是:

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

或者,您可以在激活步骤之前运行设置pyenv的相关命令(可以在.bashrc中找到它们)。对我来说,应该是:

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