typescript 云运行:用户提供的容器无法启动并侦听PORT=8080提供的定义端口

j2datikz  于 2023-01-27  发布在  TypeScript
关注(0)|答案(2)|浏览(96)

我正在尝试将容器化的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

在我的代码中,我将port声明为

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
umuewwlo

umuewwlo1#

如果您使用的是MacBook,那么下面的答案从Bk Lim在下面的链接可能会帮助你:
Cloud Run: "Failed to start and then listen on the port defined by the PORT environment variable." When I use 8080

fykwrbwg

fykwrbwg2#

更新:我把我的docker-compose文件改成了我在GitHub上找到的模板here,成功地部署了它
我的码头知识是最低限度的,所以如果有人有任何想法,为什么我的老码头组成不工作,我很想知道。

相关问题