Bash 大括号扩展不适用于 Dockerfile 运行 命令
Bash brace expansion not working on Dockerfile RUN command
我在我的 Dockerfile 中 运行 以下 运行 命令,期望在每个列出的子目录下创建一个 "logs" 目录:
RUN mkdir -p /opt/seagull/{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}/logs
但是当我检查图像时,我看到一个目录,字面意思是“{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro- env,xcap-env}" 在 /opt/seagull 下创建,而不是进行大括号扩展。
我可能做错了什么?
您没有使用大括号扩展,因为您没有使用 Bash。如果您查看 documentation for the RUN command:
RUN (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
还有:
Note: To use a different shell, other than ‘/bin/sh’, use the exec form passing in the desired shell. For example, RUN ["/bin/bash", "-c", "echo hello"]
因此,只需将命令更改为使用 exec 形式并显式使用 Bash shell:
RUN [ "/bin/bash", "-c", "mkdir -p /opt/seagull/{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}/logs" ]
如果 /bin/bash
在您的映像中可用,您可以更改 docker 构建系统用来执行您的 RUN
命令的 shell,如下所示:
SHELL ["/bin/bash", "-c"]
现在,您的 RUN
命令应该可以正常工作。
我在我的 Dockerfile 中 运行 以下 运行 命令,期望在每个列出的子目录下创建一个 "logs" 目录:
RUN mkdir -p /opt/seagull/{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}/logs
但是当我检查图像时,我看到一个目录,字面意思是“{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro- env,xcap-env}" 在 /opt/seagull 下创建,而不是进行大括号扩展。
我可能做错了什么?
您没有使用大括号扩展,因为您没有使用 Bash。如果您查看 documentation for the RUN command:
RUN (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
还有:
Note: To use a different shell, other than ‘/bin/sh’, use the exec form passing in the desired shell. For example, RUN ["/bin/bash", "-c", "echo hello"]
因此,只需将命令更改为使用 exec 形式并显式使用 Bash shell:
RUN [ "/bin/bash", "-c", "mkdir -p /opt/seagull/{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}/logs" ]
如果 /bin/bash
在您的映像中可用,您可以更改 docker 构建系统用来执行您的 RUN
命令的 shell,如下所示:
SHELL ["/bin/bash", "-c"]
现在,您的 RUN
命令应该可以正常工作。