Nginx 容器无法在云端启动 运行

Nginx container fails to start on 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 specifically listening on $PORT the docker-entrypoint.sh performs $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

云运行"runs"很好

执行替换的代码:

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 到同一个文件,正如 运行 在本地容器中测试的那样。

预计

实际

通过替换

修复
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

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

我已经发布了一篇博客 post 来展示如何 运行 在云 运行 容器中使用 nginx(以及一个进程)。

您可以在这里阅读文章:https://ahmet.im/blog/cloud-run-multiple-processes-easy-way/ or take a look at the code repository at 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 运行.

上发生变化