Cloud Run 错误:容器无法启动

Ron*_*hes 5 dockerfile google-cloud-run

我无法将基本的 Angular 应用程序部署到 Google Cloud Run。该错误表明它未在端口 8080 上正确提供服务,但在我的机器上本地运行 localhost:8080 显示该应用程序。所以我可能需要一些额外的东西来运行云,如果有人有什么想法吗?

详细情况如下:

我创建了一个基本的角度应用程序

ng new test-app
Run Code Online (Sandbox Code Playgroud)

Dockerfile 如下

FROM node:latest as node
WORKDIR /app
COPY . .

RUN npm install
RUN npm run build --prod

ENV PORT=8080

FROM nginx:latest
COPY --from=node /app/dist/test-app /usr/share/nginx/html
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]
Run Code Online (Sandbox Code Playgroud)

在本地我运行构建的容器,我可以在 localhost:8080 看到它

docker container run -p 8080:80 gcr.io/$GOOGLE_PROJECT/test-app:$IMAGE
Run Code Online (Sandbox Code Playgroud)

截屏

然后我部署到 Google Cloud Run 托管。

gcloud run deploy test-app --image gcr.io/$GOOGLE_PROJECT/test-app:$IMAGE --platform managed
Run Code Online (Sandbox Code Playgroud)

但是,它无法以错误开头

Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.
Run Code Online (Sandbox Code Playgroud)

日志中没有其他错误。

谢谢。

我从如何在与 docker 一起使用时更改 nginx 的端口中获取了有效的解决方案

我创建了 nginx.conf 文件,将端口设置为 8080,将服务器设置为 0.0.0.0

# on alpine, copy to /etc/nginx/nginx.conf
user                            root;
worker_processes                auto;

error_log                       /var/log/nginx/error.log warn;

events {
    worker_connections          1024;
}

http {
    include                     /etc/nginx/mime.types;
    default_type                application/octet-stream;
    sendfile                    off;
    access_log                  off;
    keepalive_timeout           3000;
    server {
        listen                  8080;
        root                    /usr/share/nginx/html;
        index                   index.html;
        server_name             0.0.0.0;
        client_max_body_size    16m;
    }
}
Run Code Online (Sandbox Code Playgroud)

并更新了 Dockerfile 以复制此文件。

FROM node:latest as node
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build --prod

ENV PORT=8080

FROM nginx:alpine
COPY --from=node /app/dist/streamin-app/ /usr/share/nginx/html/
COPY nginx.conf /etc/nginx/
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]
Run Code Online (Sandbox Code Playgroud)

mar*_*doi 5

我认为与所有网络接口上的容器监听有关。

根据官方文档:

Cloud Run 服务无法启动的一个常见原因是容器内的服务器进程配置为侦听本地主机 (127.0.0.1) 地址。这是指环回网络接口,容器外部无法访问该网络接口,因此无法进行Cloud Run健康检查,导致服务部署失败。

要解决此问题,请将应用程序配置为启动 HTTP 服务器以侦听所有网络接口(通常表示为 0.0.0.0)。

  • 实际的解决方案在哪里?你必须为此编写什么代码,更新什么脚本? (13认同)