#Dockerizing Nestjs properly - how do I do it?
46 messages · Page 1 of 1 (latest)
I don't know if the build succeeded or if the path to the dist folder is incorrect
This is the docker file
run
npm run build
locally
and then see how the dist folder looks like
here's the dockerfile I've been using so far for production: https://gist.github.com/micalevisk/9ee47135b22f284dd73de6ea6d5aa864
Have any ideas on how to run prisma migrate on a dockerized nest js api
ill try it, thanks.
Hi , I tried the tutorial you suggested and received the same error Cannot find module '/app'.
🤔 I've made and followed that tutorial few times...
which OS are you using?
actually, I think I know what could be wrong. Show us your tsconfig.json
and what's the value you have in the "main" entry of your package.json?
this is important since you'll run node . (taking that Gist in mind)
one min please🙏
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true
}
}
tsconfig.json
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
did u mean to this section?
the entry main of your package.json
i dont have a main entry
then you didn't followed that gist :p
hmm
anyway, that's just to make it run with node .
not needed
the issue is that your dockerfile expects the compiled version to be located at dist/main.js
it's pretty easy to fix
just see what's the file structure that you got when building your project
npm run build
npm ls dist
is there any src/main.js in there?
sure
then add
"main": "dist/src/main" to your package.json
and then the CMD will be ["node", "."]
ok
give me a min
thanks a lot for your help~
!
Is it possible that the docker is not building the dist folder?
# Base image
FROM node:18
# Create app directory
WORKDIR /usr/src/app
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
# Install app dependencies
RUN npm install
ENV NODE_ENV production
# Bundle app source
COPY . .
# Creates a "dist" folder with the production build
RUN npm run build
# Start the server using the production build
CMD [ "node", "dist/main.js" ]
This docker has already worked for me on the Google Cloud run service.
FROM node:18-slim AS base
RUN mkdir /api
RUN chown node:node /api
USER node
WORKDIR /api
COPY --chown=node:node package.json ./
# Install production dependencies
FROM base AS dependencies
COPY --chown=node:node package-lock.json ./
RUN npm ci --production
# Install development dependencies and build
FROM dependencies AS build
RUN npm ci
COPY --chown=node:node src ./src
COPY --chown=node:node nest-cli.json tsconfig.build.json tsconfig.json ./
RUN npm run build
# Copy production dependencies, build artifacts and runtime configuration and run
FROM base
COPY --chown=node:node --from=dependencies /api/node_modules ./node_modules
COPY --chown=node:node --from=build /api/dist ./dist
ENV NODE_ENV=production
CMD npm run start:prod
It suddenly stopped working
ill try it know
before you put it on some server, test if it's working locally
a lot of over bugs
'@faker-js/faker faker is dev dependency
and your production app is using it, that's why
besides that, looks like everything is working now