I’m trying to deploy my API to Cloud Run but I’m stuck with this error
ERROR: (gcloud.run.deploy) The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.
This is my Dockerfile
FROM node:lts
WORKDIR /src
COPY package.json package*.json ./
RUN npm install --omit=dev
COPY . .
CMD [ "npm", "execute" ]
This are my package.json scripts
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start": "env-cmd -x -f ./src/config/env/.env.local nodemon ./src/index.js",
"deploy:dev": "env-cmd -x -f ./src/config/env/.env.dev ./deploy.sh",
"execute": "env-cmd -x -f ./src/config/env/.env.dev node ./src/index.js"
},
This is my index.js
const api = require("../src/config/config");
const port = process.env.PORT || 8080;
console.log("Puerto => ", port);
api.listen(port, () => {
console.log(`Rest API started succesfully`);
});
This is my config file (I’m working with firebase)
const express = require("express");
// Config
const api = express();
api.use(express.json());
// Routes
api.use(require("../routes/start.routes.js"));
module.exports = api;
And I have a .env file with the PORT variable
PORT=8080
And these are the commands I execute
gcloud builds submit --tag gcr.io/$GOOGLE_PROJECT_ID/api --project=$GOOGLE_PROJECT_ID
gcloud run deploy api --image gcr.io/$GOOGLE_PROJECT_ID/api --port 8080 --platform managed --region us-central1 --allow-unauthenticated --project=$GOOGLE_PROJECT_ID
I have followed every tip about similar questions but none has worked for me
I checked logs and they only expose the description I referred in the beginning.
I try to run my project locally with Cloud Run Emulator, it does not work but I don’t get enough info to figure out what’s wrong. I don’t even understand why in the docker container I see several ports except 8080 which is the one the app should listen on and then says the deploy process failed
I’m using windows 11
My API works fine locally if I run npm run start
2
Answers
You need to troubleshoot the issue
that command starts your container and map port 8080 inside the container to your local machine’s port 8080. Access your app at http://localhost:8080. Check the console output for any errors with the container.
in your index.js file Make sure the api.listen() function is using a port variable that is set to the value of the PORT environment variable:
some times firewall blocks traffic to port 8080.
you can check the firewall rules in the Google Cloud Console
using that step you find the issues and if you can’t find the issue then reach out to the Cloud Run support team for further assistance.
The error clearly does indicate that there is an issue with the specific defined incoming HTTP requests ports and the container is failing to listen on the expected port.The official document Cloud Run container contract has these mentioned to meet these requirements in order to operate properly.
In Node.js ,your js should define as below:
const port = process.env.PORT || 8080; app.listen(port, () => { console.log('Hello listening port', port); });
You may check if your container is listening on all network interfaces once by denoting port as
0.0.0.0
and see if that works.You may also want to confirm if your container image is compiled for 64-bit Linux as expected by the container runtime contract.