【发布时间】:2019-10-12 13:19:00
【问题描述】:
我正在尝试在 Cloud Run 上使用 Nginx 提供简单的静态页面。但是容器无法正确开始服务。
容器正在启动,如从docker-entrypoint.sh 回显的调试行所示:
2019-05-26T22:19:02.340289Z testing config
2019-05-26T22:19:02.433935Z nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2019-05-26T22:19:02.434903Z nginx: configuration file /etc/nginx/nginx.conf test is successful
2019-05-26T22:19:02.436605Z starting on 8080
2019-05-26T22:19:02.487188Z2019/05/26 22:19:02 [alert] 6#6: prctl(PR_SET_DUMPABLE) failed (22: Invalid argument)
并最终终止
2019-05-26T22:20:00.153060259ZContainer terminated by the container manager on signal 9.
为了符合Cloud Run service contract 专门监听$PORT 的要求,docker-entrypoint.sh 执行$PORT substitution in conf.d/*.conf。
FROM nginx:1.15-alpine
COPY nginx-default.conf.template /etc/nginx/conf.d/default.conf.template
COPY docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]
我很确定问题在于 docker-entrypoint.sh,因为一旦 $PORT 被硬编码为 8080 并且图像看起来像这样:
FROM nginx:1.15-alpine
COPY nginx-default.conf /etc/nginx/conf.d/default.conf
Cloud Run“运行”正常。
执行替换的代码:
export NGINX_PORT=${PORT:-8080}
for f in $(find /etc/nginx/conf.d/ -type f -name '*.conf'); do
envsubst '$NGINX_PORT' < $f > $f
done
注意:读取< $f 和写入> $f 到同一个文件的工作方式与本地运行容器的测试结果相同。
预期
- nginx 配置将
$PORT占位符替换为实际值 - 容器在 Cloud Run 上运行并在
$PORT上侦听
实际
- 容器无法在 Cloud Run 上运行
- 容器在本地运行并侦听
$PORT
【问题讨论】:
-
您的脚本 (/docker-entrypoint.sh) 是否以类似于 #!/bin/bash 的 shebang 开头?
-
在你的代码中你的问题是 COPY nginx-default.conf.template /etc/nginx/conf.d/default.conf.template .... 你是不是意思...配置文件的正确目的地?那如何成为真正的 nginx 配置文件?
-
你的假设是错误的。不要这样做
NOTE: reading < $f and writing > $f -
相反,对您的文件进行就地编辑,如下所示:
sed -i 's/80/${PORT}/g' filename。这是一个示例,不是为您的配置文件编写的。 -
你是说问题解决了吗?
标签: google-cloud-run