gpt4 book ai didi

docker - if else 与 dockerfile 指令

转载 作者:行者123 更新时间:2023-12-02 18:21:05 26 4
gpt4 key购买 nike

我有一个这样的 Dockerfile:

FROM ubuntu:16.04
:
:
RUN git clone <url>
:

有时,我要克隆的存储库太大。

So, before running this git clone command, I would like to check whether the repo that I want to clone, exists in the current directory( directory of dockerfile ) and if it does, then I would do a COPY <repo-name> . instruction instead of the RUN git clone <url> instruction.

我想实现这样的目标:

FROM ubuntu:16.04
:
:
if exists <repo-name>
COPY <repo-name>
else
RUN git clone <url>
:

有可能吗?如果没有,我可以采取什么技巧来实现这一目标?

最佳答案

If-Else 不是 Dockerfiles 中的东西。您最好在 RUN 中使用单行 bash 命令提供了 If-Else:

RUN if git clone <repo>; then echo "Exists"; else echo "Error"; fi

或者,如果您所追求的行为需要更多脚本编写,请将其放入脚本中并以这种方式运行,以防止 Dockerfile 中出现所有困惑情况:

COPY git-script.sh /usr/local/bin
RUN chmod +x /usr/local/bin/git-script.sh && \
bash git-script.sh

找到类似答案here

关于docker - if else 与 dockerfile 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52932650/

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