Deploy on GCP
Hey, i'm deploying my NestJS application on GCP cloud run, using Prisma, but I still have this error :
Does anyone have an idea ?
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.
However, I did what was necessary regarding environment variables, here is my entry point in NestJS :
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import * as dotenv from 'dotenv'
import { BadRequestException, Logger, ValidationPipe } from '@nestjs/common'
import { SwaggerExcludeFilter } from './swagger/swagger-exclude-filter.service'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const cookieParser = require('cookie-parser')
async function bootstrap() {
dotenv.config()
const app = await NestFactory.create(AppModule, { cors: true })
await SwaggerExcludeFilter.setup(app)
app.enableCors({
origin: process.env.URL_FRONT,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204,
credentials: true
})
app.useLogger(new Logger())
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
disableErrorMessages: false,
exceptionFactory: errors => {
return new BadRequestException(
errors.map(
error =>
`${error.property} errors: ${Object.values(
error.constraints
).join(', ')}`
)
)
}
})
)
app.use(cookieParser())
const PORT = process.env.PORT || 8080
const HOSTNAME = process.env.HOSTNAME || '0.0.0.0'
await app.listen(PORT, HOSTNAME)
}
bootstrap()
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import * as dotenv from 'dotenv'
import { BadRequestException, Logger, ValidationPipe } from '@nestjs/common'
import { SwaggerExcludeFilter } from './swagger/swagger-exclude-filter.service'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const cookieParser = require('cookie-parser')
async function bootstrap() {
dotenv.config()
const app = await NestFactory.create(AppModule, { cors: true })
await SwaggerExcludeFilter.setup(app)
app.enableCors({
origin: process.env.URL_FRONT,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204,
credentials: true
})
app.useLogger(new Logger())
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
disableErrorMessages: false,
exceptionFactory: errors => {
return new BadRequestException(
errors.map(
error =>
`${error.property} errors: ${Object.values(
error.constraints
).join(', ')}`
)
)
}
})
)
app.use(cookieParser())
const PORT = process.env.PORT || 8080
const HOSTNAME = process.env.HOSTNAME || '0.0.0.0'
await app.listen(PORT, HOSTNAME)
}
bootstrap()
3 Replies
Also i'm running prisma in the Dockerfiles :
# Development stage
FROM node:20-bullseye-slim AS development
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
COPY . .
RUN npx prisma generate
RUN yarn build
USER node
# Production stage (clean image)
FROM node:20-bullseye-slim AS production
# Same repo as development stage
WORKDIR /usr/src/app
# Copy from development stage
COPY --from=development /usr/src/app .
CMD ["yarn", "start:prod"]
# Development stage
FROM node:20-bullseye-slim AS development
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
COPY . .
RUN npx prisma generate
RUN yarn build
USER node
# Production stage (clean image)
FROM node:20-bullseye-slim AS production
# Same repo as development stage
WORKDIR /usr/src/app
# Copy from development stage
COPY --from=development /usr/src/app .
CMD ["yarn", "start:prod"]
Hey š
Does this work as expected in your local environment?
You only get this error when deploying to GCP?
On my local environnement it doesnt work also with docker when i'm using the GCP database URL :
Part of the problem looks to comes from the URL format :
ā nest-api git:(docker-img) docker build -t my-nestjs-app .
[+] Building 10.4s (14/14) FINISHED docker:desktop-linux
...
=> [development 1/7] FROM docker.io/... 0.0s
=> CACHED [development 2/7] WORKDIR /usr/src/app
=> CACHED [development 3/7] COPY package.json yarn.lock ./
=> CACHED [development 4/7] RUN yarn install --frozen-lockfile
=> [development 5/7] COPY . .
=> [development 6/7] RUN npx prisma generate
=> [development 7/7] RUN yarn build
=> CACHED [production 3/4] RUN ls -la /usr/src/app
=> [production 4/4] COPY --from=development /usr/src/app .
=> exporting to image
=> => exporting layers
=> => writing image sha256:123456... 0.0s
=> => naming to docker.io/library/my-nestjs-app
ā nest-api git:(docker-img) docker build -t my-nestjs-app .
[+] Building 10.4s (14/14) FINISHED docker:desktop-linux
...
=> [development 1/7] FROM docker.io/... 0.0s
=> CACHED [development 2/7] WORKDIR /usr/src/app
=> CACHED [development 3/7] COPY package.json yarn.lock ./
=> CACHED [development 4/7] RUN yarn install --frozen-lockfile
=> [development 5/7] COPY . .
=> [development 6/7] RUN npx prisma generate
=> [development 7/7] RUN yarn build
=> CACHED [production 3/4] RUN ls -la /usr/src/app
=> [production 4/4] COPY --from=development /usr/src/app .
=> exporting to image
=> => exporting layers
=> => writing image sha256:123456... 0.0s
=> => naming to docker.io/library/my-nestjs-app
ā nricher-nest-api git:(docker-img) docker run -p 8080:8080 my-nestjs-app
yarn run v1.22.19
$ yarn prisma migrate deploy && node dist/src/main.js
$ /usr/src/app/node_modules/.bin/prisma migrate deploy
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "mydatabase", schema "public" at "34.175.... (ect)"
Error: P1001: Can't reach database server at `/cloudsql/nricher-nest-...`:`5432`
Please make sure your database server is running at `/cloudsql/nricher-nest-....
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ā nest-api git:(docker-img)
ā nricher-nest-api git:(docker-img) docker run -p 8080:8080 my-nestjs-app
yarn run v1.22.19
$ yarn prisma migrate deploy && node dist/src/main.js
$ /usr/src/app/node_modules/.bin/prisma migrate deploy
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "mydatabase", schema "public" at "34.175.... (ect)"
Error: P1001: Can't reach database server at `/cloudsql/nricher-nest-...`:`5432`
Please make sure your database server is running at `/cloudsql/nricher-nest-....
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ā nest-api git:(docker-img)