gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:55 25 4
gpt4 key购买 nike

我有一个 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 session ,则源确实有效:

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

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

我做了一些挖掘,最初看起来问题可能出在 bash 作为 Ubuntu login shelldash 之间的区别 作为 Ubuntu 系统 shelldash 不支持 source 命令。

但是,这个问题的答案似乎是使用 '.' 而不是 source,但这只会导致 Docker 运行时因 go panic 异常而崩溃.

从 Dockerfile RUN 指令运行 shell 脚本以解决此问题的最佳方法是什么(我正在运行 Ubuntu 12.04 LTS 的默认基础镜像)。

最佳答案

原始答案

FROM ubuntu:14.04
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

这应该适用于每个 Ubuntu docker 基础镜像。我通常会为我编写的每个 Dockerfile 添加这一行。

相关旁观者编辑

如果您想获得“在整个 Dockerfile 中使用 bash 而不是 sh” 的效果,而无需改变可能会损坏* 容器内的操作系统,你可以 tell Docker your intention .这样做是这样的:

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

* The possible damage is that many scripts in Linux (on a fresh Ubuntu install grep -rHInE '/bin/sh' / returns over 2700 results) expect a fully POSIX shell at /bin/sh. The bash shell isn't just POSIX plus extra builtins. There are builtins (and more) that behave entirely different than those in POSIX. I FULLY support avoiding POSIX (and the fallacy that any script that you didn't test on another shell is going to work because you think you avoided basmisms) and just using bashism. But you do that with a proper shebang in your script. Not by pulling the POSIX shell out from under the entire OS. (Unless you have time to verify all 2700 plus scripts that come with Linux plus all those in any packages you install.)

下面这个答案中有更多详细信息。 https://stackoverflow.com/a/45087082/117471

关于bash - 在带有 'source' 的 Dockerfile 中使用 RUN 指令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50749966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com