【问题标题】:Cloud Run: The user-provided container failed to start and listen on the port defined provided by the PORT=8080Cloud Run:用户提供的容器启动失败,监听PORT=8080提供的端口
【发布时间】:2023-01-26 05:45:05
【问题描述】:

我正在尝试将容器化的 node-typescript-express 应用程序部署到云运行,但我无法这样做,收到以下错误:

The user-provided container failed to start and listen on the port defined provided by the PORT=8080

这是我的 Dockerfile 配置:

FROM node:18.13.0 as base

WORKDIR /home/node/app

COPY package*.json ./

RUN npm i

COPY . .

FROM base as production

ENV NODE_PATH=./dist

RUN npm run build

在我的代码中,我将端口声明为

const PORT = process.env.PORT || 8080;

我还有一个 .env 文件,我在其中设置了端口,但我删除了端口密钥——据我所知,GCP 云运行无论如何都会注入端口变量。

这是我在 GCP 上的项目设置的屏幕截图。我通过使用docker-compose build 在本地构建它来上传我的图像,标记它,然后将它上传到 GCP 容器存储库。

我试过在代码中手动设置端口,完全删除 env 文件,指定不同的端口等。我什至不确定端口是否是特定错误,它只是某种包罗万象。

这是我的package.json

{
  "name": "weather-service",
  "version": "0.0.0",
  "description": "small node server that fetches openweather api data",
  "engines": {
    "node": ">= 18.12 <19"
  },
  "scripts": {
    "start": "NODE_PATH=./dist node dist/src/index.js",
    "clean": "rimraf coverage dist tmp",
    "dev": "ts-node-dev -r tsconfig-paths/register src/index.ts",
    "prebuild": "npm run lint",
    "build": "ttsc -p tsconfig.release.json",
    "build:watch": "ttsc -w -p tsconfig.release.json",
    "build:release": "npm run clean && ttsc -p tsconfig.release.json",
    "test": "jest --coverage --detectOpenHandles --forceExit",
    "test:watch": "jest --watch --detectOpenHandles --forceExit",
    "lint": "eslint . --ext .ts --ext .mts && tsc",
    "lint:fix": "eslint . --ext .ts --ext .mts",
    "prettier": "prettier --config .prettierrc --write .",
    "prepare": "husky install",
    "pre-commit": "lint-staged"

最后,这是我的 docker-compose 文件以及我如何执行命令

docker-compose.yml

version: '3.7'

services:
  weather-service:
    build:
      context: .
      dockerfile: Dockerfile
      target: base
    volumes:
      - ./src:/home/node/app/src
    container_name: weather-service
    expose:
      - '8080'
    ports:
      - '8080:8080'
    command: npm run dev

docker-compose.prod.yml

version: '3.7'

services:
  weather-service:
    build:
      target: production
    command: npm run start

docker.compose.dev.yml

version: '3.7'

services:
  weather-service:
    env_file:
      - .env
    environment:
      - ${PORT}
      - ${WEATHER_API_KEY}

Makefile

up:
    docker-compose -f docker-compose.yml -f docker-compose.dev.yml up

up-prod:
    docker-compose -f docker-compose.yml -f docker-compose.prod.yml up

down: 
    docker-compose down

build:
    docker-compose build

【问题讨论】:

    标签: node.js typescript express google-cloud-platform


    【解决方案1】:

    如果您使用的是 Macbook,那么以下链接中 Bk Lim 的回答可能会对您有所帮助:

    Cloud Run: "Failed to start and then listen on the port defined by the PORT environment variable." When I use 8080

    【讨论】:

    • 看到那篇文章,但运气不好,如果重要的话,我在 Ubuntu 上。
    【解决方案2】:

    更新:我通过将我的 Dockerfile 和 docker-compose 文件更改为我在 GitHub 上找到的模板,成功部署了它,here

    我的 docker 知识很少,所以如果有人知道为什么我的旧 Dockerfile 不起作用,我很想知道。

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2021-06-13
      • 2021-11-10
      • 1970-01-01
      • 2021-05-24
      • 2021-05-13
      • 2021-11-10
      • 2021-10-07
      • 2010-12-08
      相关资源
      最近更新 更多