node-microservices icon indicating copy to clipboard operation
node-microservices copied to clipboard

fix: build node_modules into separate docker layer

Open leon opened this issue 4 years ago • 1 comments

To optimize the docker build we want to build as little as possible. by first installing node_modules and then copying the app and starting it. We can cache a lot.

the first build took 68s and then since it can cache it only takes 25s. If we where to enable the nx cloud build, it would drop to < 10s.

I also added some convenience scripts and changes the html port to 80 so you can have the main app run on http://localhost

Sending build context to Docker daemon    287MB
Step 1/10 : FROM node:lts-alpine
 ---> 471e8b4eb0b2
Step 2/10 : WORKDIR /app
 ---> Using cache
 ---> e490029e0f70
Step 3/10 : ENV NODE_ENV production
 ---> Using cache
 ---> 40f401406616
Step 4/10 : ENV PORT 3334
 ---> Using cache
 ---> 187fd4f8b583
Step 5/10 : EXPOSE ${PORT}
 ---> Using cache
 ---> 81f52c510071
Step 6/10 : COPY package.json ./
 ---> Using cache
 ---> 1a20d9abd4e3
Step 7/10 : RUN npm install --production
 ---> Using cache
 ---> b66a3cb03a58
Step 8/10 : RUN npm install reflect-metadata tslib rxjs @nestjs/platform-express --production
 ---> Using cache
 ---> 350dd7802dbd
Step 9/10 : COPY ./dist/apps/html .
 ---> Using cache
 ---> d27c958c2e68
Step 10/10 : CMD ["node", "main.js"]
 ---> Using cache
 ---> 11fdd7a87f6a
Successfully built 11fdd7a87f6a
Successfully tagged html:latest
Sending build context to Docker daemon    287MB
Step 1/10 : FROM node:lts-alpine
 ---> 471e8b4eb0b2
Step 2/10 : WORKDIR /app
 ---> Using cache
 ---> e490029e0f70
Step 3/10 : ENV NODE_ENV production
 ---> Using cache
 ---> 40f401406616
Step 4/10 : ENV PORT 3333
 ---> Using cache
 ---> 81cd12d5da72
Step 5/10 : EXPOSE ${PORT}
 ---> Using cache
 ---> b8f3856dd45d
Step 6/10 : COPY package.json ./
 ---> Using cache
 ---> 65cda8747490
Step 7/10 : RUN npm install --production
 ---> Using cache
 ---> a76d5a5c9860
Step 8/10 : RUN npm install reflect-metadata tslib rxjs @nestjs/platform-express --production
 ---> Using cache
 ---> d24cad6a4250
Step 9/10 : COPY ./dist/apps/api .
 ---> 55b003c33c8a
Step 10/10 : CMD ["node", "main.js"]
 ---> Running in cdf158e5b780
Removing intermediate container cdf158e5b780
 ---> acb3be1e26ea
Successfully built acb3be1e26ea
Successfully tagged api:latest

leon avatar Feb 05 '21 16:02 leon

this is far better approach! 👍🏻

surajkrishan avatar Jan 09 '23 16:01 surajkrishan