小编典典

在带有'source'的Dockerfile中使用RUN指令不起作用

docker

我有一个Dockerfile,我正在将其安装在一起以安装Vanilla 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

如果我ls进入该目录(只是为了测试是否已完成前面的步骤),我可以看到文件按预期存在:

$ 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

如果我尝试仅运行source命令,则会收到与上述相同的“未找到”错误。但是,如果我运行交互式shell会话,则源代码确实可以工作:

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

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

我已经做了一些挖掘,初步看起来好像问题可能在于之间的差别 的bash 作为Ubuntu的 登录shell破折号 作为Ubuntu的
系统外壳仪表板 不支持的source命令。

但是,答案似乎是使用 “。”。 而不是source,但这只会导致Docker运行时因发生紧急恐慌而崩溃。

从Dockerfile RUN指令运行Shell脚本来解决此问题的最佳方法是什么(在Ubuntu 12.04 LTS的默认基础映像上运行)。


阅读 1043

收藏
2020-06-17

共1个答案

小编典典

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

2020-06-17