我正在尝试在 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“运行”良好。
执行替换的代码:
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)
注意:读取< $f和写入> $f同一文件的工作原理是通过本地运行容器进行测试的。
预期的
$PORT占位符替换为实际值$PORT容器在 Cloud Run 上运行和侦听实际的
$PORT容器在本地运行并监听我发布了一篇博客文章,展示如何在 Cloud Run 容器中运行 nginx(以及进程)。
您可以在这里阅读这篇文章: https: //ahmet.im/blog/cloud-run-multiple-processes-easy-way/或查看代码存储库https://github.com/ahmetb/multi-进程容器惰性解决方案
基本上,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;
您可以对端口 8080 进行安全的硬编码,nginx.conf因为在可预见的将来,它在 Cloud Run 上不太可能发生更改。
| 归档时间: | 
 | 
| 查看次数: | 2326 次 | 
| 最近记录: |