Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable
Asked Answered
R

14

63

I built my container image, but when I try to deploy it from the gcloud command line or the Cloud Console, I get the following error: "Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable."

Ramakrishna answered 13/4, 2019 at 5:7 Comment(0)
R
43

In your code, you probably aren't listening for incoming HTTP requests, or you're listening for incoming requests on the wrong port.

Or you might not have a start script and modules setup on your package.json if you are using Node.js.

As documented in the Cloud Run container runtime contract, your container must listen for incoming HTTP requests on the port that is defined by Cloud Run and provided in the $PORT environment variable.

If your container fails to listen on the expected port, the revision health check will fail, the revision will be in an error state and the traffic will not be routed to it.

For example, in Node.js with Express, you should use:

// index.js
const port = process.env.PORT || 8080;
app.listen(port, () => {
  console.log('Hello world listening on port', port);
});


// package.json
    "engines": {
        "node": "16.x"
    },
    "scripts": {
        "start": "node index.js"
    },

In Go:

port := os.Getenv("PORT")
if port == "" {
        port = "8080"
   }
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))

In python:

app.run(port=int(os.environ.get("PORT", 8080)),host='0.0.0.0',debug=True)
Ramakrishna answered 13/4, 2019 at 5:7 Comment(7)
adding this 'ENV PORT 8080 ENV HOST 0.0.0.0' to dockerfile is not a good idea? like this paste.ubuntu.com/p/ccKB5khCyJCorral
what is the code if we are not using express js only node js?Corral
one more where we have paste that express code? server.js ? or docker file config possible? this way ?scotch.io/tutorials/how-to-deploy-a-node-js-app-to-herokuCorral
how to write this for a vue js project? github.com/Timtech4u/node-cloud-run-cd/blob/master/index.js we need to find index.js in config/index.js?Corral
Where should I put this line in my python code? app.run(port=int(os.environ.get("PORT", 8080)),host='0.0.0.0',debug=True)Anjanette
Can do this in the Dockerfile instead?Aarika
In my case I had included an essential dependency of an npm run script in devDependencies. GCR does not include devDependencies so the script failed. "All dependencies that you define under the devDependencies field are ignored and do not get installed for your app in App Engine." cloud.google.com/appengine/docs/standard/nodejs/… Lesson? Check your logs.Backpack
N
15

Try this, it worked in mine. Somehow you need to change the underlying platform it builts image upon.

docker buildx build --platform linux/amd64 -t {project-name} .

Reference: https://vincenttechblog.com/fixed-cloud-run-failed-to-start-and-then-listen-on-the-port-defined-by-the-port/

Nuli answered 28/2, 2023 at 15:35 Comment(3)
If you've built on an ARM-based Mac, this does the correct cross-compilation for Cloud Run's linux/amd64 environmentSisely
I'm on a Mac M2 chip and this was the only thing that worked for me!Now
Came here to say this after finding this same article on my own. So relieved!Grantgranta
A
4

I had this issue because I built the container using on an ARM based mac (M1). Building it for --platform linux/amd64 solved it for me. Google docs also say using cloud build will sort it.

https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start

Alterative answered 20/1 at 19:14 Comment(0)
O
3

One of the other reason may be the one which I observed. Docker images may not have the required code to run the application.

I had a Node application written in TypeScript. In order to dockerize the application all I need to do is compile the code tsc and run docker build but I though that gcloud builds submit will be taking care of that and picking the compiled code as the Dockerfile suggested in conjunction to the .dockerignore and will build my source code and submit to the repository.

But what all it did was to copy my source code and submitted to the Cloud Build and there as per the Dockerfile it dockerized my source code as compared to dockerizing the compiled code.

So remember to include a build step in Dockerfile if you are doing a source code in a language with require compilation.

  • Remember that enabling the build step in the Dockerfile will increase the image size every time you do a image push to the repository. It is eating the space over there and google is going to charge you for that.
Oversize answered 25/4, 2019 at 17:24 Comment(0)
D
3

I was exposing a PORT in dockerfile , remove that automatically fixed my problem. Google injects PORT env variable so the project will pick up that Env variable.

Damara answered 20/4, 2022 at 11:53 Comment(0)
H
2

Another possibility is that the docker image ends with a command that takes time to complete. By the time deployment starts the server is not yet running and the health check will hit a blank.

What kind of command would that be ? Usually any command that runs the server in dev mode. For Scala/SBT it would be sbt run or in Node it would be something like npm run dev. In short make sure to run only on the packaged build.

Hydrogenolysis answered 22/3, 2020 at 18:47 Comment(1)
Any idea how long a container has to start?Penitence
D
2

In my case this simply occurred when the app crashed and had nothing to do with the PORT. I had the process.env defined properly. There should be a link to a log record when the error is thrown, click on it and see what happened there first.

Edit: Also, it happened that my application server (Fastify) did not accept any request inside the Docker container without explicitly setting host to 0.0.0.0 but worked locally when I started the app using yarn. As silly it may sound, make sure your container is behaves as expected locally.

Dugald answered 18/7, 2023 at 8:7 Comment(0)
D
1

We can also specify the port number used by the image from the command line. If we are using Cloud Run, we can use the following:

gcloud run deploy --image gcr.io/<PROJECT_ID>/<APP_NAME>:<APP_VERSION> --max-instances=3 --port <PORT_NO>

Where

  • <PROJECT_ID> is the project ID
  • <APP_NAME> is the app name
  • <APP_VERSION> is the app version
  • <PORT_NO> is the port number
Dejesus answered 27/10, 2022 at 10:12 Comment(1)
You are so welcome, Mamun.Dejesus
N
0

The Cloud Run is generating default yaml file which has hard-coded default port in it:

spec:
  containerConcurrency: 80
  timeoutSeconds: 300
  containers:
  - image: us.gcr.io/project-test/express-image:1.0
    ports:
    - name: http1
      containerPort: 8080
    resources:
      limits:
        memory: 256Mi
        cpu: 1000m 

So, we need to expose the same 8080 port or change the containerPort in yaml file and redeploy.

Here is more about that:

Narvik answered 16/1, 2021 at 23:42 Comment(0)
I
0

A possible solution could be:

  1. build locally
  2. push the image on google cloud
  3. deploy on google run

With commands:

docker build -t gcr.io/project-name/image-name
docker push gcr.io/project-name/image-name
gcloud run deploy tag-name --image gcr.io/project-name/image-name
Isochronism answered 6/12, 2022 at 13:55 Comment(0)
U
0

This usually occurs when you have connected your docker image to cloud sql instance's public ip address within the code instead of private ip address

Untruthful answered 16/5, 2023 at 10:26 Comment(0)
S
0

I had the same issue and after trying a lot of things, I realized that there was one missing dependency (In my case was the openai dependency) under my package.json file.

I solved it by installing it locally with:

npm i openai
Scissors answered 30/12, 2023 at 23:44 Comment(0)
D
-2

In my case, I got this error when trying to deploy a GEN 2 function using a Terraform script.

The function that did not deploy had the functions-framework listed as a DEV DEPENDENCY. The error dissapeared when I moved it to the regular DEPENDENCY list

DO THIS

"dependencies": {
   "@google-cloud/functions-framework": "^3.2.0",
}

NOT THIS

"devDpendencies": {
   "@google-cloud/functions-framework": "^3.2.0",
}
Dermoid answered 27/6, 2023 at 14:5 Comment(0)
N
-2

The Problem doesn't have anything in common with project config or cloud setting. Try to lookup the logs in cloud run. For me it worked.

In my case I triggered a function outside of the firebase-platform by using its own packages which my cloud function doesn't include. So I needed to install the necessary packages and it worked fine.

Niobic answered 4/7 at 17:10 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Cityscape

© 2022 - 2024 — McMap. All rights reserved.