【发布时间】:2022-01-21 11:49:48
【问题描述】:
当我尝试用这行代码构建 Dockerfile 时:
RUN modifyPermissions() {if [ -n "${OWNER}" ]; then \
chown "${OWNER}":zagrebgroup -R /var/log/app /mnt;\
else\
chown 1001:zagrebgroup -R /var/log/app /mnt;\
fi\
}
我有这个错误:
/bin/sh: 1: Syntax error: "}" unexpected
看起来问题出在最后一个大括号中,但我不明白为什么。 请帮忙!天呐!
【问题讨论】:
-
为什么要在 Dockerfile 中声明一个 shell 函数?你不能直接运行
if [ -n "${OWNER}" ] ; …吗?或者甚至只是RUN chown "${OWNER:-1001}":zagrebgroup -R /var/log/app /mnt? -
试试
{后面的空格 -
一个 shell 函数将在该 shell 的持续时间内存在,在这种情况下,它将在 RUN 步骤结束时被丢弃。当您修复语法错误时,这不太可能获得您想要的结果。
-
感谢您的回答,根据cmets,最重要的是不要在Dockerfile中声明函数。我想使用这些功能,但您不建议在 cmets 中使用它。我有很多代码,我想将它们划分为函数以便更清晰地执行,并且我的代码不会重复自己。
-
我尝试在 { 之后添加空格。得到错误:/bin/sh: 1: [[: not found /bin/sh: 1: Syntax error: "then" unexpected (expecting "fi")
标签: docker dockerfile sh