问题
I am failing to get a basic Angular app deploying to Google Cloud Run. The error would suggest it is not being served correctly at port 8080, but running locally on my machine localhost:8080 displays the app. So possibly I need something extra for cloud run, if anybody has some idea?
The details are as follows:
I create a basic angular app
ng new test-app
The Dockerfile is as follows
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;"]
Locally I run the built container and I can see it at localhost:8080
docker container run -p 8080:80 gcr.io/$GOOGLE_PROJECT/test-app:$IMAGE
screenshot
I then deploy to Google Cloud Run managed.
gcloud run deploy test-app --image gcr.io/$GOOGLE_PROJECT/test-app:$IMAGE --platform managed
However, it fails to start with the error
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.
There are no other errors in the logs.
Thanks.
The solution which worked I took from How to change the port of nginx when using with docker
I created the nginx.conf file, setting the port to 8080 & server to 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;
}
}
And updated the Dockerfile to copy over this file.
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;"]
回答1:
I think is related to container listening on all network interfaces.
According to the official documentation:
A common reason for Cloud Run services failing to start is that the server process inside the container is configured to listen on the localhost (127.0.0.1) address. This refers to the loopback network interface, which is not accessible from outside the container and therefore Cloud Run health check cannot be performed, causing the service deployment failure.
To solve this, configure your application to start the HTTP server to listen on all network interfaces, commonly denoted as 0.0.0.0.
回答2:
“docker container run -p 8080:80 gcr.io/$GOOGLE_PROJECT/test-app:$IMAGE”
shows you mapped port 8080 of your local machine to port 80 for your image, which indicates that your image is listening on port 80.
You can either 1) change your image or 2) deploy to cloud run with custom port:
gcloud alpha run deploy ... -port 80
来源:https://stackoverflow.com/questions/59524020/cloud-run-error-container-failed-to-start