简体   繁体   中英

How do I make sure GCloud is running node?

I have been trying to deploy a Node.js app on Gcloud run but it keeps coming up with the error: The user-provided container failed to start and listen on the port defined provided by the PORT=5000 environment variable .

I was playing around with the port variable and app.listen but that all seemed like it was fine, it was listening on the correct port. Then I checked the logs to see if there was any more information and before the actual error, there was a warning: bash: node: command not found but I've specified node: >=18.0.0 in the "engines" part of the package.json file and nodejs18 in app.yaml . Is there something else I'm supposed to do that I can't find in the docs?

In the code I am using the following to serve the web page:

this.app.listen(process.env.PORT, () => {
    console.log(`Server Listening on port ${process.env.PORT}`);
});

And here's my YAML file from GCloud:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name:
  namespace:
  selfLink: 
  resourceVersion: 
  generation: 14
  creationTimestamp: '2023-01-17T23:15:41.934438Z'
  labels:
    managed-by: gcp-cloud-build-deploy-cloud-run
    gcb-trigger-id: 
    commit-sha: 
    gcb-build-id: 
    cloud.googleapis.com/location: europe-west1
  annotations:
    run.googleapis.com/client-name: gcloud
    serving.knative.dev/creator:
    serving.knative.dev/lastModifier: 
    client.knative.dev/user-image:
    run.googleapis.com/client-version: 415.0.0
    run.googleapis.com/operation-id:
    run.googleapis.com/ingress: all
    run.googleapis.com/ingress-status: all
spec:
  template:
    metadata:
      name:
      labels:
        managed-by: gcp-cloud-build-deploy-cloud-run
        gcb-trigger-id:
        commit-sha:
        gcb-build-id:
      annotations:
        run.googleapis.com/client-name: gcloud
        client.knative.dev/user-image: 
        run.googleapis.com/client-version: 415.0.0
        autoscaling.knative.dev/maxScale: '100'
    spec:
      containerConcurrency: 80
      timeoutSeconds: 300
      serviceAccountName:
      containers:
      - image: 
        ports:
        - name: http1
          containerPort: 8080
        resources:
          limits:
            cpu: 1000m
            memory: 512Mi
  traffic:
  - percent: 100
    latestRevision: true
status:
  observedGeneration: 14
  conditions:
  - type: Ready
    status: 'False'
    reason: HealthCheckContainerError
    message: "Revision '' is not ready and cannot serve traffic. 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.\n\nLogs URL: 
      \ \nFor more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start"
    lastTransitionTime: '2023-01-26T12:36:36.279097Z'
  - type: ConfigurationsReady
    status: 'False'
    reason: HealthCheckContainerError
    message: "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.\n\nLogs URL: 
      \ \nFor more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start"
    lastTransitionTime: '2023-01-26T12:36:36.279097Z'
  - type: RoutesReady
    status: 'False'
    reason: HealthCheckContainerError
    message: "Revision '' is not ready and cannot serve traffic. 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.\n\nLogs URL: 
      \ \nFor more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start"
    lastTransitionTime: '2023-01-26T12:36:36.208550Z'
  latestReadyRevisionName: 
  latestCreatedRevisionName: 
  traffic:
  - revisionName: 
    percent: 100
    latestRevision: true
  url: 
  address:
    url: 

Make sure your nodejs app does start on the port your container is exposing and running. I would suggest to use the default 8080 for troubleshooting.

# In Dockerfile

EXPOSE 8080
CMD [ "node", "app.js" ]
# in .env file

PORT=8080
// In nodejs (assuming expressjs framework)

//load the .env
require('dotenv').config();

app.listen(process.env.PORT, () => {
  console.log(`Running on ${process.env.PORT}`);
});
# deploy command

gcloud run deploy <YOUR_SERVICE_NAME> \
    --image <YOUR_DOCKER_IMAGE_URL> \
    --region <YOUR_PROJECT_REGION> \
    --allow-unauthenticated

Also, can you share the app.yml file? Looks like you docker base image does not include nodejs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM