我有一个Dockerfile,我正在一起安装一个香草python环境(我将在其中安装一个应用程序,但在晚些时候)。

FROM ubuntu:12.04

# required to build certain python libraries
RUN apt-get install python-dev -y

# install pip - canonical installation instructions from pip-installer.org
# http://www.pip-installer.org/en/latest/installing.html
ADD https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py /tmp/ez_setup.py
ADD https://raw.github.com/pypa/pip/master/contrib/get-pip.py /tmp/get-pip.py
RUN python /tmp/ez_setup.py
RUN python /tmp/get-pip.py
RUN pip install --upgrade pip 

# install and configure virtualenv
RUN pip install virtualenv 
RUN pip install virtualenvwrapper
ENV WORKON_HOME ~/.virtualenvs
RUN mkdir -p $WORKON_HOME
RUN source /usr/local/bin/virtualenvwrapper.sh

构建运行正常,直到最后一行,在那里我得到以下异常:

[previous steps 1-9 removed for clarity]
...
Successfully installed virtualenvwrapper virtualenv-clone stevedore
Cleaning up...
 ---> 1fc253a8f860
Step 10 : ENV WORKON_HOME ~/.virtualenvs
 ---> Running in 8b0145d2c80d
 ---> 0f91a5d96013
Step 11 : RUN mkdir -p $WORKON_HOME
 ---> Running in 9d2552712ddf
 ---> 3a87364c7b45
Step 12 : RUN source /usr/local/bin/virtualenvwrapper.sh
 ---> Running in c13a187261ec
/bin/sh: 1: source: not found

如果我进入该目录(只是为了测试之前的步骤是否已提交),我可以看到文件如预期的那样存在:

$ docker run 3a87 ls /usr/local/bin
easy_install
easy_install-2.7
pip
pip-2.7
virtualenv
virtualenv-2.7
virtualenv-clone
virtualenvwrapper.sh
virtualenvwrapper_lazy.sh

如果我试着运行源代码命令,我得到相同的'not found'错误如上所示。如果我运行一个交互式shell会话,但是,源确实工作:

$ docker run 3a87 bash
source
bash: line 1: source: filename argument required
source: usage: source filename [arguments]

我可以从这里运行脚本,然后愉快地访问workon, mkvirtualenv等。

我做了一些研究,最初看起来问题可能在于bash作为Ubuntu登录shell,而dash作为Ubuntu系统shell, dash不支持源命令。

然而,这个问题的答案似乎是使用'。'而不是source,但这只会导致Docker运行时爆发一个go panic异常。

从Dockerfile run指令运行shell脚本来解决这个问题的最好方法是什么(我运行Ubuntu 12.04 LTS的默认基本映像)。


当前回答

根据https://docs.docker.com/engine/reference/builder/#run,运行的默认[Linux] shell是/bin/sh -c。您似乎希望使用bashisms,因此应该使用RUN的“执行表单”来指定您的shell。

RUN ["/bin/bash", "-c", "source /usr/local/bin/virtualenvwrapper.sh"]

否则,使用RUN的“shell形式”并指定不同的shell会导致嵌套的shell。

# don't do this...
RUN /bin/bash -c "source /usr/local/bin/virtualenvwrapper.sh"
# because it is the same as this...
RUN ["/bin/sh", "-c", "/bin/bash" "-c" "source /usr/local/bin/virtualenvwrapper.sh"]

如果你有超过一个命令需要不同的shell,你应该阅读https://docs.docker.com/engine/reference/builder/#shell并将其放在RUN命令之前更改默认shell:

SHELL ["/bin/bash", "-c"]

最后,如果您在根用户的.bashrc文件中放置了您需要的任何东西,您可以在SHELL或RUN命令中添加-l标志,使其成为登录SHELL,并确保它得到源代码。

注意:我故意忽略了这样一个事实,即将脚本作为RUN中的唯一命令是没有意义的。

其他回答

我最终把我的env的东西放在。profile和突变SHELL之类的东西

SHELL ["/bin/bash", "-c", "-l"]

# Install ruby version specified in .ruby-version
RUN rvm install $(<.ruby-version)

# Install deps
RUN rvm use $(<.ruby-version) && gem install bundler && bundle install

CMD rvm use $(<.ruby-version) && ./myscript.rb

如果您只是试图使用pip将某些东西安装到virtualenv中,您可以修改PATH env以首先查看virtualenv的bin文件夹

ENV PATH=“/path/to/venv/bin:${PATH}”

然后Dockerfile中的任何pip安装命令都将首先找到/path/to/venv/bin/pip并使用它,这将安装到virtualenv而不是系统python中。

根据https://docs.docker.com/engine/reference/builder/#run,运行的默认[Linux] shell是/bin/sh -c。您似乎希望使用bashisms,因此应该使用RUN的“执行表单”来指定您的shell。

RUN ["/bin/bash", "-c", "source /usr/local/bin/virtualenvwrapper.sh"]

否则,使用RUN的“shell形式”并指定不同的shell会导致嵌套的shell。

# don't do this...
RUN /bin/bash -c "source /usr/local/bin/virtualenvwrapper.sh"
# because it is the same as this...
RUN ["/bin/sh", "-c", "/bin/bash" "-c" "source /usr/local/bin/virtualenvwrapper.sh"]

如果你有超过一个命令需要不同的shell,你应该阅读https://docs.docker.com/engine/reference/builder/#shell并将其放在RUN命令之前更改默认shell:

SHELL ["/bin/bash", "-c"]

最后,如果您在根用户的.bashrc文件中放置了您需要的任何东西,您可以在SHELL或RUN命令中添加-l标志,使其成为登录SHELL,并确保它得到源代码。

注意:我故意忽略了这样一个事实,即将脚本作为RUN中的唯一命令是没有意义的。

我也有同样的问题。如果你也使用python基础映像,你可以将shell脚本中的shebang行更改为#!/bin/bash。 例如,请参阅Manuel Lazo的container_entrypoint.sh。

我也遇到了同样的问题,为了在virtualenv中执行pip install,我必须使用以下命令:

RUN pip install virtualenv virtualenvwrapper
RUN mkdir -p /opt/virtualenvs
ENV WORKON_HOME /opt/virtualenvs
RUN /bin/bash -c "source /usr/local/bin/virtualenvwrapper.sh \
    && mkvirtualenv myapp \
    && workon myapp \
    && pip install -r /mycode/myapp/requirements.txt"

我希望这能有所帮助。