我正在尝试在 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 服务合同,专门监听$PORT
在.docker-entrypoint.sh
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
占位符替换为实际值 - 容器
$PORT
在 Cloud Run 上运行和侦听
实际的
- 容器无法在 Cloud Run 上运行
$PORT
容器在本地运行和监听