【问题标题】:How can I execute a .sh file after cd command in a dockerfile?如何在 dockerfile 中的 cd 命令之后执行 .sh 文件?
【发布时间】:2020-08-19 20:28:00
【问题描述】:

我正在尝试在 python 基础 docker 映像中安装 Libressl。 Python 镜像默认有 openssl。

我的Dockerfile 代码:

FROM python:3.7

RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install git cpp make dh-autoreconf -y
RUN pip3 install requests
RUN git clone https://github.com/libressl-portable/portable.git /portable
RUN cd /portable \
       ./autogen.sh \
       ./configure --prefix=/opt/libressl --enable-nc \
       make check \
       make install

RUN echo "alias openssl='/opt/libressl/bin/openssl'" >> ~/.bashrc

COPY . /app
WORKDIR /app
CMD ["python3", "./debug.py"]

但是,我发现 git clone 做得很好,但是下一个命令失败了。

即使autogen.sh 似乎也没有被执行。

我如何获取 .bashrc 文件?

当我使用source ~/.bashrc 时,找不到源命令,因为该命令使用/bin/sh 运行。

我的Dockerfile 可能有什么问题?

谢谢:)

【问题讨论】:

  • 您可以使用括号RUN ( cd /portable && ./autogen.sh && ./configure )创建一个子shell
  • 除了你得到的答案,我会删除提到.bashrc 的行。大多数运行 Docker 容器的路径根本不会运行它,而且 Python 脚本在任何情况下都不能使用 shell 别名。考虑使用默认的--prefix=/usr/local 将库安装到“系统”目录中; Docker 映像与主机系统隔离,因此不会发生冲突。

标签: docker dockerfile docker-image


【解决方案1】:

要组合多个命令调用,请使用运算符&&。此外,您可以对两个目标使用一个拨打电话

cd /portable && \
       ./autogen.sh && \
       ./configure --prefix=/opt/libressl --enable-nc && \
       make check install

【讨论】:

    【解决方案2】:

    @alexander 的回答很棒。 另一种方法是:从 Dockerfile 执行 shell 脚本,并将所有 shell 命令放在一个地方。使 Dockerfile 更优雅。

    例如:

    FROM python:3.7
    COPY . /app
    RUN ./app/script.sh
    WORKDIR /app
    CMD ["python3", "./debug.py"]
    

    和 script.sh(您编写为简单的 shell 脚本)(复制\过去您发布的内容,无需测试):

    apt-get update
    DEBIAN_FRONTEND=noninteractive apt-get install git cpp make dh-autoreconf -y
    pip3 install requests
    git clone https://github.com/libressl-portable/portable.git /portable
    cd /portable \
           ./autogen.sh \
           ./configure --prefix=/opt/libressl --enable-nc \
    make check
    make install
    
    echo "alias openssl='/opt/libressl/bin/openssl'" >> ~/.bashrc
    

    【讨论】:

    • 很遗憾,您的 shell 脚本无法运行。它需要 && 运算符,只要将命令与 `\` 组合在一行中即可
    猜你喜欢
    • 2019-07-01
    • 2015-06-07
    • 1970-01-01
    • 2017-05-21
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    相关资源
    最近更新 更多