Nginx容器无法在Cloud Run上启动

时间:2019-05-26 23:05:27

标签: google-cloud-run

我正在尝试在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上监听

2 个答案:

答案 0 :(得分:1)

通过替换固定

for f in $(find /etc/nginx/conf.d/ -type f -name '*.conf'); do
  envsubst '$NGINX_PORT' < $f > $f
done

使用

sed -i "s/\${NGINX_PORT}/${NGINX_PORT}/g" /etc/nginx/conf.d/*.conf

并在$NGINX_PORT文件中更改${NGINX_PORT}-> *.conf,以避免替换歧义

答案 1 :(得分:1)

我已经发表了一篇博客文章,展示了如何在Cloud Run容器(以及进程)中运行nginx。

您可以在此处阅读文章:https://ahmet.im/blog/cloud-run-multiple-processes-easy-way/或在https://github.com/ahmetb/multi-process-container-lazy-solution上查看代码存储库

基本上,nginx.conf文件应类似于:

events {}

http {
    server {
        listen 8080; # Cloud Run PORT env variable
        access_log /dev/stdout;
        error_log /dev/stdout;

        # if you need to serve static access, specify an absolute path like below
        location /static/ {
            alias /src/static/;
        }

        # anything else is routed to your app that you would start on port 8081
        location / {
            proxy_pass http://localhost:8081;
        }
    }
}

daemon off;
pid /run/nginx.pid;

您可以在nginx.conf中使用安全的硬编码端口8080,因为在可预见的将来Cloud Run上不太可能更改它。