docker 如何使用带引号的新命令提交 docker

docker how to commit a docker with a new command with quotation signs

在制作 docker 的过程中,我必须将其命令从 /bin/sh 更改为 nginx -g "daemon off;"(正是这样)。

我写了:

docker commit --change="EXPOSE 80" --change='CMD ["nginx", "-g", "\"daemon off;\""]' ${arr[0]} mine/nginx_final

其中 ${arr[0]} 扩展到正确的 docker 容器。

然而,当我尝试 运行 这个 docker 它失败并显示错误:

nginx: [emerg] unexpected end of parameter, expecting ";" in command line

Docker 检查也没有发现任何问题:

        "Cmd": [
            "nginx",
            "-g",
            "\"daemon off;\""
        ],

预期,我希望 "\"daemon off;\"" 扩展到 "daemon off;"

但我很确定 deamon off 后面有一个 ; 符号。这个标志去哪儿了?我该如何调试呢? (并修复它)

Nginx 无法处理包含引号的全局指令:"daemon off;"

docker commit \
  --change='EXPOSE 80' \
  --change='CMD ["nginx", "-g", "daemon off;"]' \
  ${arr[0]} \
  mine/nginx_final

执行表格

CMD ["foo"] is called the exec form. A process will be run via exec 而不是通过 shell。数组中的每个元素都成为 exec 的参数。额外的 " 引号正在传递给 nginx:

CMD ["nginx", "-g", "\"daemon off;\""]
exec('nginx', '-g', '"daemon off;"')

使用 exec 形式已经通过 space 不变,所以您只需要:

CMD ["nginx", "-g", "daemon off;"]
exec('nginx' '-g' 'daemon off;')

Shell表格

CMD foo 称为 shell 形式。 spaces 的全局指令参数需要在此处引用:

CMD nginx -g "daemon off;"
exec('sh', '-c', 'nginx -g "daemon off;"')
exec('nginx', '-g', 'daemon off;')

否则 shell 解释命令将拆分 space 上的参数并尝试使用 3 个参数执行 nginx

CMD nginx -g daemon off;
exec('sh', '-c', 'nginx -g daemon off;')
exec('nginx', '-g', 'daemon', 'off;')