#Dockerizing Nestjs properly - how do I do it?

46 messages · Page 1 of 1 (latest)

shy sonnet
#

There are many examples of how to Dockerize my NestJS app, but when I run the docker file, I get the same result. "Cannot find module '/usr/src/app/dist/main.js'"
Is there anyone who can help me with this?

#

I don't know if the build succeeded or if the path to the dist folder is incorrect

slate charm
#

run
npm run build
locally
and then see how the dist folder looks like

cedar bear
#

Have any ideas on how to run prisma migrate on a dockerized nest js api

shy sonnet
slate charm
#

🤔 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)

shy sonnet
#

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?

slate charm
shy sonnet
#

i dont have a main entry

slate charm
#

then you didn't followed that gist :p

shy sonnet
#

hmm

slate charm
#

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

shy sonnet
#

this is the structure

slate charm
#

is there any src/main.js in there?

shy sonnet
#

sure

slate charm
#

then add
"main": "dist/src/main" to your package.json
and then the CMD will be ["node", "."]

shy sonnet
#

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.

wild halo
#
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
shy sonnet
#

It suddenly stopped working

wild halo
#

before you put it on some server, test if it's working locally

shy sonnet
#

a lot of over bugs

#

'@faker-js/faker faker is dev dependency

slate charm
#

besides that, looks like everything is working now