title: Cloud Run error: Container failed to start
body: <p>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?</p>
  
  <p>The details are as follows:</p>
  
  <p>I create a basic angular app</p>
  
  <pre><code>ng new test-app
  </code></pre>
  
  <p>The Dockerfile is as follows</p>
  
  <pre><code>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;"]
  </code></pre>
  
  <p>Locally I run the built container and I can see it at localhost:8080</p>
  
  <pre><code>docker container run -p 8080:80 gcr.io/$GOOGLE_PROJECT/test-app:$IMAGE
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/pUEc4.png" rel="noreferrer">screenshot</a></p>
  
  <p>I then deploy to Google Cloud Run managed.</p>
  
  <pre><code>gcloud run deploy test-app --image gcr.io/$GOOGLE_PROJECT/test-app:$IMAGE --platform managed
  </code></pre>
  
  <p>However, it fails to start with the error</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>There are no other errors in the logs.</p>
  
  <p>Thanks.</p>
  
  <p>The solution which worked I took from <a href="https://stackoverflow.com/questions/47364019/how-to-change-the-port-of-nginx-when-using-with-docker">How to change the port of nginx when using with docker</a></p>
  
  <p>I created the nginx.conf file, setting the port to 8080 &amp; server to 0.0.0.0</p>
  
  <pre><code># 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;
      }
  }
  </code></pre>
  
  <p>And updated the Dockerfile to copy over this file. </p>
  
  <pre><code>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;"]
  </code></pre>