我有一个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的默认基本映像)。


这可能是因为源代码是bash内置的,而不是文件系统上的二进制文件。您的意图是让您正在获取的脚本在之后更改容器吗?


原来的答案

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

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

由一个关心的旁观者编辑

如果你想要得到“在整个Dockerfile中使用bash而不是sh”的效果,而不改变和可能破坏容器内的操作系统,你可以告诉Docker你的意图。是这样做的:

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


我也遇到了同样的问题,为了在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"

我希望这能有所帮助。


您可能想要运行bash -v来查看源是什么。

我会做以下事情,而不是玩符号链接:

执行echo "source /usr/local/bin/virtualenvwrapper.sh" >> /etc/bash.bashrc .sh


根据本页上的回答,我想补充一点,您必须意识到,每个RUN语句都是使用/bin/sh -c独立运行的,因此不会获得通常在登录shell中获取的任何环境变量。

到目前为止我发现的最好的方法是将脚本添加到/etc/bash.然后调用每个命令作为bash登录。

RUN echo "source /usr/local/bin/virtualenvwrapper.sh" >> /etc/bash.bashrc
RUN /bin/bash --login -c "your command"

例如,你可以安装和设置virtualenvwrapper,创建虚拟的env,当你使用bash登录时激活它,然后将你的python模块安装到这个env中:

RUN pip install virtualenv virtualenvwrapper
RUN mkdir -p /opt/virtualenvs
ENV WORKON_HOME /opt/virtualenvs
RUN echo "source /usr/local/bin/virtualenvwrapper.sh" >> /etc/bash.bashrc
RUN /bin/bash --login -c "mkvirtualenv myapp"
RUN echo "workon mpyapp" >> /etc/bash.bashrc
RUN /bin/bash --login -c "pip install ..."

阅读bash启动文件的手册有助于理解在什么时候获取了什么。


最简单的方法是使用点操作符代替source,这是相当于bash source命令的sh:

而不是:

RUN source /usr/local/bin/virtualenvwrapper.sh

Use:

RUN . /usr/local/bin/virtualenvwrapper.sh

我在Dockerfile中运行源代码时也遇到了问题

这在构建CentOS 6.6 Docker容器时运行得非常好,但在Debian容器中出现了问题

RUN cd ansible && source ./hacking/env-setup

这就是我解决问题的方法,可能不是一种优雅的方式,但这对我来说是有效的

RUN echo "source /ansible/hacking/env-setup" >> /tmp/setup
RUN /bin/bash -C "/tmp/setup"
RUN rm -f /tmp/setup

根据Docker文档

要使用不同的shell,而不是' /bin/sh ',请使用传入所需shell的exec表单。例如, RUN ["/bin/bash", "-c", "echo hello"]

看到https://docs.docker.com/engine/reference/builder/运行


RUN指令的默认shell是["/bin/sh", "-c"]。

RUN "source file"      # translates to: RUN /bin/sh -c "source file"

使用SHELL指令,你可以改变Dockerfile中后续RUN指令的默认SHELL:

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

现在,默认shell已经更改,您不需要在每个RUN指令中显式地定义它

RUN "source file"    # now translates to: RUN /bin/bash -c "source file"

附加提示:您还可以添加——login选项,该选项将启动登录shell。这意味着~/。例如Bashrc会被读取,你不需要在你的命令之前显式地来源它


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

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

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


如果您使用的是Docker 1.12或更新版本,只需使用SHELL !

简短的回答:

一般:

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

对于python vituralenv:

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

长一点的回答:

从https://docs.docker.com/engine/reference/builder/壳

SHELL ["executable", "parameters"] The SHELL instruction allows the default shell used for the shell form of commands to be overridden. The default shell on Linux is ["/bin/sh", "-c"], and on Windows is ["cmd", "/S", "/C"]. The SHELL instruction must be written in JSON form in a Dockerfile. The SHELL instruction is particularly useful on Windows where there are two commonly used and quite different native shells: cmd and powershell, as well as alternate shells available including sh. The SHELL instruction can appear multiple times. Each SHELL instruction overrides all previous SHELL instructions, and affects all subsequent instructions. For example: FROM microsoft/windowsservercore # Executed as cmd /S /C echo default RUN echo default # Executed as cmd /S /C powershell -command Write-Host default RUN powershell -command Write-Host default # Executed as powershell -command Write-Host hello SHELL ["powershell", "-command"] RUN Write-Host hello # Executed as cmd /S /C echo hello SHELL ["cmd", "/S"", "/C"] RUN echo hello The following instructions can be affected by the SHELL instruction when the shell form of them is used in a Dockerfile: RUN, CMD and ENTRYPOINT. The following example is a common pattern found on Windows which can be streamlined by using the SHELL instruction: ... RUN powershell -command Execute-MyCmdlet -param1 "c:\foo.txt" ... The command invoked by docker will be: cmd /S /C powershell -command Execute-MyCmdlet -param1 "c:\foo.txt" This is inefficient for two reasons. First, there is an un-necessary cmd.exe command processor (aka shell) being invoked. Second, each RUN instruction in the shell form requires an extra powershell -command prefixing the command. To make this more efficient, one of two mechanisms can be employed. One is to use the JSON form of the RUN command such as: ... RUN ["powershell", "-command", "Execute-MyCmdlet", "-param1 \"c:\\foo.txt\""] ... While the JSON form is unambiguous and does not use the un-necessary cmd.exe, it does require more verbosity through double-quoting and escaping. The alternate mechanism is to use the SHELL instruction and the shell form, making a more natural syntax for Windows users, especially when combined with the escape parser directive: # escape=` FROM microsoft/nanoserver SHELL ["powershell","-command"] RUN New-Item -ItemType Directory C:\Example ADD Execute-MyCmdlet.ps1 c:\example\ RUN c:\example\Execute-MyCmdlet -sample 'hello world' Resulting in: PS E:\docker\build\shell> docker build -t shell . Sending build context to Docker daemon 4.096 kB Step 1/5 : FROM microsoft/nanoserver ---> 22738ff49c6d Step 2/5 : SHELL powershell -command ---> Running in 6fcdb6855ae2 ---> 6331462d4300 Removing intermediate container 6fcdb6855ae2 Step 3/5 : RUN New-Item -ItemType Directory C:\Example ---> Running in d0eef8386e97 Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 10/28/2016 11:26 AM Example ---> 3f2fbf1395d9 Removing intermediate container d0eef8386e97 Step 4/5 : ADD Execute-MyCmdlet.ps1 c:\example\ ---> a955b2621c31 Removing intermediate container b825593d39fc Step 5/5 : RUN c:\example\Execute-MyCmdlet 'hello world' ---> Running in be6d8e63fe75 hello world ---> 8e559e9bf424 Removing intermediate container be6d8e63fe75 Successfully built 8e559e9bf424 PS E:\docker\build\shell> The SHELL instruction could also be used to modify the way in which a shell operates. For example, using SHELL cmd /S /C /V:ON|OFF on Windows, delayed environment variable expansion semantics could be modified. The SHELL instruction can also be used on Linux should an alternate shell be required such as zsh, csh, tcsh and others. The SHELL feature was added in Docker 1.12.


根据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

如果您有可用的SHELL,您应该使用这个答案——不要使用已接受的答案,这将迫使您将dockerfile的其余部分放在每个注释的一个命令中。

如果你使用的是旧版本的Docker,没有SHELL的权限,只要你不需要.bashrc中的任何东西(这在Dockerfiles中是很少见的情况),这就可以工作:

ENTRYPOINT ["bash", "--rcfile", "/usr/local/bin/virtualenvwrapper.sh", "-ci"]

注意-i是用来让bash读取rcfile的。


下面是一个示例Dockerfile,它利用了一些聪明的技术,让你为每个run节运行一个完整的conda环境。您可以使用类似的方法来执行脚本文件中的任意准备。

注意:当涉及到登录/交互式vs非登录/非交互式shell,信号,exec,多个参数的处理方式,引用,CMD和ENTRYPOINT如何交互,以及其他一百万件事情时,有很多细微差别,所以如果在使用这些东西时,事情会发生变化,不要气馁。我花了很多令人沮丧的时间在各种文学作品中挖掘,但我仍然不太明白这一切是如何奏效的。

## Conda with custom entrypoint from base ubuntu image
## Build with e.g. `docker build -t monoconda .`
## Run with `docker run --rm -it monoconda bash` to drop right into
## the environment `foo` !
FROM ubuntu:18.04

## Install things we need to install more things
RUN apt-get update -qq &&\
    apt-get install -qq curl wget git &&\
    apt-get install -qq --no-install-recommends \
        libssl-dev \
        software-properties-common \
    && rm -rf /var/lib/apt/lists/*

## Install miniconda
RUN wget -nv https://repo.anaconda.com/miniconda/Miniconda3-4.7.12-Linux-x86_64.sh -O ~/miniconda.sh && \
    /bin/bash ~/miniconda.sh -b -p /opt/conda && \
    rm ~/miniconda.sh && \
    /opt/conda/bin/conda clean -tipsy && \
    ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh

## add conda to the path so we can execute it by name
ENV PATH=/opt/conda/bin:$PATH

## Create /entry.sh which will be our new shell entry point. This performs actions to configure the environment
## before starting a new shell (which inherits the env).
## The exec is important! This allows signals to pass
RUN     (echo '#!/bin/bash' \
    &&   echo '__conda_setup="$(/opt/conda/bin/conda shell.bash hook 2> /dev/null)"' \
    &&   echo 'eval "$__conda_setup"' \
    &&   echo 'conda activate "${CONDA_TARGET_ENV:-base}"' \
    &&   echo '>&2 echo "ENTRYPOINT: CONDA_DEFAULT_ENV=${CONDA_DEFAULT_ENV}"' \
    &&   echo 'exec "$@"'\
        ) >> /entry.sh && chmod +x /entry.sh

## Tell the docker build process to use this for RUN.
## The default shell on Linux is ["/bin/sh", "-c"], and on Windows is ["cmd", "/S", "/C"]
SHELL ["/entry.sh", "/bin/bash", "-c"]
## Now, every following invocation of RUN will start with the entry script
RUN     conda update conda -y

## Create a dummy env
RUN     conda create --name foo

## I added this variable such that I have the entry script activate a specific env
ENV CONDA_TARGET_ENV=foo

## This will get installed in the env foo since it gets activated at the start of the RUN stanza
RUN  conda install pip

## Configure .bashrc to drop into a conda env and immediately activate our TARGET env
RUN conda init && echo 'conda activate "${CONDA_TARGET_ENV:-base}"' >>  ~/.bashrc
ENTRYPOINT ["/entry.sh"]

我曾经处理过一个用Django web框架开发的应用程序的类似场景,这些步骤对我来说非常有效:

我的Dockerfile内容

[mlazo@srvjenkins project_textile]$ cat docker/Dockerfile.debug 
FROM malazo/project_textile_ubuntu:latest 

ENV PROJECT_DIR=/proyectos/project_textile PROJECT_NAME=project_textile WRAPPER_PATH=/usr/share/virtualenvwrapper/virtualenvwrapper.sh

COPY . ${PROJECT_DIR}/
WORKDIR ${PROJECT_DIR}

RUN echo "source ${WRAPPER_PATH}" > ~/.bashrc
SHELL ["/bin/bash","-c","-l"]
RUN     mkvirtualenv -p $(which python3) ${PROJECT_NAME} && \
        workon ${PROJECT_NAME} && \
        pip3 install -r requirements.txt 

EXPOSE 8000

ENTRYPOINT ["tests/container_entrypoint.sh"]
CMD ["public/manage.py","runserver","0:8000"]

ENTRYPOINT文件"tests/container_entrypoint.sh"的内容:

[mlazo@srvjenkins project_textile]$ cat tests/container_entrypoint.sh
#!/bin/bash
# *-* encoding : UTF-8 *-*
sh tests/deliver_env.sh
source ~/.virtualenvs/project_textile/bin/activate 
exec python "$@"

最后,我部署容器的方式是:

[mlazo@srvjenkins project_textile]$ cat ./tests/container_deployment.sh 
#!/bin/bash

CONT_NAME="cont_app_server"
IMG_NAME="malazo/project_textile_app"
[ $(docker ps -a |grep -i ${CONT_NAME} |wc -l) -gt 0 ] && docker rm -f ${CONT_NAME} 
docker run --name ${CONT_NAME} -p 8000:8000 -e DEBUG=${DEBUG} -e MYSQL_USER=${MYSQL_USER} -e MYSQL_PASSWORD=${MYSQL_PASSWORD} -e MYSQL_HOST=${MYSQL_HOST} -e MYSQL_DATABASE=${MYSQL_DATABASE} -e MYSQL_PORT=${MYSQL_PORT}  -d ${IMG_NAME}

我真的希望这能对其他人有所帮助。

问候,


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


这是我在Ubuntu 20.04上的解决方案

RUN apt -y update
RUN apt -y install curl
SHELL ["/bin/bash", "-c"]
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
RUN source /root/.bashrc
RUN bash -c ". /root/.nvm/nvm.sh && nvm install v16 && nvm alias default v16 && nvm use default"