云运行错误:容器无法启动

时间:2019-12-29 22:54:28

标签: angular dockerfile google-cloud-run

我无法将基本的Angular应用部署到Google Cloud Run。该错误将表明端口8080不能正确为其提供服务,但是在我的计算机localhost:8080上本地运行会显示该应用程序。因此,如果有人有想法,我可能需要一些额外的资源来运行云?

详细信息如下:

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

ng new test-app

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;"]

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

docker container run -p 8080:80 gcr.io/$GOOGLE_PROJECT/test-app:$IMAGE

screenshot

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

gcloud run deploy test-app --image gcr.io/$GOOGLE_PROJECT/test-app:$IMAGE --platform managed

但是,它无法从错误开始

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.

日志中没有其他错误。

谢谢。

我从How to change the port of nginx when using with docker那里获得的解决方案

我创建了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;
    }
}

并更新了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;"]

3 个答案:

答案 0 :(得分:1)

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

根据官方文档:

  

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

     

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

答案 1 :(得分:1)

“ docker container run -p 8080:80 gcr.io/$GOOGLE_PROJECT/test-app:$IMAGE”

显示您将本地计算机的端口8080映射到映像的端口80,这表示映像正在侦听端口80。

您可以1)更改映像或2)部署到使用自定义端口运行的云:

gcloud alpha运行部署...-端口80

答案 2 :(得分:0)

  1. 顺便说一句,我根本没有搞砸 nginx
  2. 我的 docker 文件中只有 EXPOSE 4200
  3. 我在此处将容器端口 4200 添加到容器的设置中:

enter image description here

然后它给了我一个位于顶部的 URL:

enter image description here

这给了我一个无效的主机头错误,我通过重新部署我的容器修复了这个错误:

CMD ng serve --host 0.0.0.0 --disable-host-check

请参阅有关此 here 的讨论。