Docker 运行 exec 形式的命令不起作用

Docker RUN command in exec form not working

我正在构建一个基于 Alpine 的 docker 图像。

FROM alpine

RUN apk update \
    && apk add lighttpd \
    && rm -rf /var/cache/apk/*

ENV COLOR red

COPY ./index.html /var/www/localhost/htdocs

RUN /bin/ash -c 'echo abcd'
#working
RUN /bin/ash -c "echo $COLOR; sed -i -e 's/red/$COLOR/g' /var/www/localhost/htdocs/index.html; cat /var/www/localhost/htdocs/index.html;"
#not working
# RUN ["sh", "-c", "echo $COLOR; sed -i -e 's/red/$COLOR/g' /var/www/localhost/htdocs/index.html; cat /var/www/localhost/htdocs/index.html;"]

CMD ["lighttpd","-D","-f","/etc/lighttpd/lighttpd.conf"]

当我 运行 以 shell 形式工作时它工作正常,但是当我 运行 以 exec 形式它给出

/bin/sh: [sh,: not found

我尝试使用 bin/shshbin/ashash。所有人都犯同样的错误。

我已经使用 Docker 几年了,我不知道(直到你的问题)有 shell|RUN 的执行表格 ;-)

问题是您的命令包含环境变量 ($COLOR),并且 exec 形式没有替代|求值。

参见:

https://docs.docker.com/engine/reference/builder/#run

“与 shell 形式不同,exec 形式不调用命令 shell。这意味着正常的 shell 处理不会发生”

Shell负责扩展变量,但只有双引号内的变量才会被扩展。

你的错误来自于$COLOR之前的错误\,其实你从shell取值是没有意义的,正确的做法是:[=15] =]

RUN ["sh", "-c", "echo $COLOR; sed -i -e \"s/red/$COLOR/g\" /var/www/localhost/htdocs/index.html; cat /var/www/localhost/htdocs/index.html;"]

显示效果的最小示例,仅供参考:

Dockerfile:

FROM alpine
ENV COLOR rednew
RUN echo "red" > /tmp/index.html
RUN ["sh", "-c", "sed -i -e \"s/red/$COLOR/g\" /tmp/index.html; cat /tmp/index.html;"]

结果:

$ docker build -t abc:1 . --no-cache                                                                                              
Sending build context to Docker daemon  5.632kB
Step 1/4 : FROM alpine
 ---> 28f6e2705743
Step 2/4 : ENV COLOR rednew
 ---> Running in 05c43146fab0
Removing intermediate container 05c43146fab0
 ---> 28ea1434e626
Step 3/4 : RUN echo "red" > /tmp/index.html
 ---> Running in 2c8fbbc5fd10
Removing intermediate container 2c8fbbc5fd10
 ---> f884892ad8c4
Step 4/4 : RUN ["sh", "-c", "sed -i -e \"s/red/$COLOR/g\" /tmp/index.html; cat /tmp/index.html;"]
 ---> Running in 6930b3d03438
rednew
Removing intermediate container 6930b3d03438
 ---> b770475672cc
Successfully built b770475672cc
Successfully tagged abc:1