DEV Community

How to Dockerize your NestJS App for production

Abbas Ogaji on December 28, 2019

As we all know Docker is not just another buzzword, but one of the best containerization tool for software engineers. With the ability to ship any ...
Collapse
 
hehehai profile image
hehehai

I don't know why this is? , I followed the instructions on the tutorial, but I tried it, and it still didn't solve it!

Step 11/11 : CMD ["npm", "run", "start:prod"]
 ---> Running in e5f55126e787
Removing intermediate container e5f55126e787
 ---> 6a07a1a8b24a
Successfully built 6a07a1a8b24a
Successfully tagged bd-url-query_nest:latest
Creating bd-url-query ... done
Attaching to bd-url-query
bd-url-query |
bd-url-query | > bd-link@0.0.1 start:prod /app
bd-url-query | > node dist/main.js
bd-url-query |
bd-url-query | internal/modules/cjs/loader.js:1032
bd-url-query |   throw err;
bd-url-query |   ^
bd-url-query |
bd-url-query | Error: Cannot find module '/app/dist/main.js'
bd-url-query |     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
bd-url-query |     at Function.Module._load (internal/modules/cjs/loader.js:898:27)
bd-url-query |     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
bd-url-query |     at internal/main/run_main_module.js:17:47 {
bd-url-query |   code: 'MODULE_NOT_FOUND',
bd-url-query |   requireStack: []
bd-url-query | }
bd-url-query | npm ERR! code ELIFECYCLE
bd-url-query | npm ERR! errno 1
bd-url-query | npm ERR! bd-link@0.0.1 start:prod: `node dist/main.js`
bd-url-query | npm ERR! Exit status 1
bd-url-query | npm ERR!
bd-url-query | npm ERR! Failed at the bd-link@0.0.1 start:prod script.
bd-url-query | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
bd-url-query | npm WARN Local package.json exists, but node_modules missing, did you mean to install?
bd-url-query |
bd-url-query | npm ERR! A complete log of this run can be found in:
bd-url-query | npm ERR!     /root/.npm/_logs/2020-06-03T14_10_18_871Z-debug.log
bd-url-query exited with code 1
Enter fullscreen mode Exit fullscreen mode
Collapse
 
abbasogaji profile image
Abbas Ogaji • Edited

You might be missing some build dependencies using the alpine version, you can use this instead;

FROM node:10
WORKDIR /app
COPY ./package.json ./
RUN npm install
COPY . .
RUN npm run build
# EXPOSE 3000
CMD ["npm", "run", "start:prod"]

Collapse
 
hehehai profile image
hehehai

Sorry, I did not post the complete configuration. I tried it and found that it was a problem with WORKDIR. It was normal when I ran it for the first time. This problem occurred the second time docker-compose up. is normal. I am trying to find out why.

FROM node:latest

WORKDIR /app/bd-url-query

COPY package*.json .
COPY yarn.lock .

RUN yarn

COPY . .
RUN yarn prebuild && yarn build

CMD [ "node", "dist/main.js"]
Thread Thread
 
abbasogaji profile image
Abbas Ogaji • Edited

" Error: Cannot find module '/app/dist/main.js" it was looking main.js at "/app/dist/main.js" instead of "/app/bd-url-query/dist/main.js"

Just take a look at what you have in your directory, add the ls command i.e "RUN ls -l" before "CMD....." line in your Dockerfile and inspect the files you have there

Collapse
 
seand52 profile image
seand52

Nice post!

Just a small comment - I'm fairly certain that it's not necessary to do it in two steps by using first the full node image and then node-alpine. I got it working with just 1 step using node-alpine for everything:

FROM node:10-alpine
WORKDIR /app
COPY ${PWD}/package.json ./
RUN yarn
COPY . .
RUN yarn build
EXPOSE 5000
CMD ["sh", "-c", "yarn typeorm migration:run && yarn start:prod"]

Collapse
 
abbasogaji profile image
Abbas Ogaji • Edited

Very true, although they might be scenarios where your dependencies rely on native builds, and that might require you to install build tools like node-gpy (which is written in python [meaning also need to install python] ), make, gcc, g++ which might not exists on alpine version,

Although you can still get away with it by installing build tools via apk in Dockerfile

Collapse
 
goodluck11 profile image
goodluck11

This man...thanks for the article... this is Goodluck

Collapse
 
abbasogaji profile image
Abbas Ogaji

Mahnn how u doing👍

Collapse
 
goodluck11 profile image
goodluck11

Very good man and you?

Collapse
 
bezerrarichard profile image
bezerraRichard

Thanks for the post! it was a lot of help.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
abbasogaji profile image
Abbas Ogaji

thanks man

Collapse
 
balvinder294 profile image
Balvinder Singh

Well explained, nice article.

Collapse
 
leonardom profile image
leo

Thanks a lot for sharing!!! :)

Collapse
 
thavoo profile image
Gustavo Herrera

Good post. Thanks

Collapse
 
sonofab1rd profile image
Jacob Dierkens

Brilliant! Thank you!!

Collapse
 
hungleq profile image
hungleQ

Hi Buddy.
This post is really good. It fixed my issue.
Thanks mate.

Collapse
 
francoislp profile image
François

I was looking for a quick Dockerfile to test FluxCD with a simple NestJS hello world. Thanks for the snippet!

Collapse
 
varsharautjee profile image
varsha

I have two apps:admin and agent and they are on 3000 and 3001 port.
npm run build agent
npm run build admin needs to be done .. how should I proceed?