Multistage dockerfile in nodejs
Multistage docker in nodejs
As an Intro step
At the beginning of this small essay center-around Docker MultiStage some prerequisites are highly important.
Docker and actually its medium level of knowledge
Facing with necessity of multistage as important affair in production of application
Virtualization
In first look you can see this statement in most of virtualizer tools Run operating systems for any machine, on any supported architecture. Fascinating happens while you deal with many diffrent infrastructure, tools and restriction which are fully incongruous or need hard shoes to config them to roll out service harmony and insync.
Basically virtual machines open new floor for engineer to run process in fully isolated mode without any coupling with primary OS. So this great and handy manner regardless of legacy application age or dependencies and their freshness, automatically restrict developer to stable behavior with nearly zero hiatus on application development life cycle. Some utitlization like QEMU, libvirt, VMware Workstation and many more.
Some topics like How to up and runing project on vm is trends which recent years have been seen more likely and present the fact that spread range of new technology try to cope with Virtualization.
Docker and containerization era
I can assert confidently that rarely happen take a look at repos and don’t see Dockerfile there. Exceeding 4.7 million available Dockerfile in 2.2 million repository just shows docker has broadly accepted among programming communities.
Dockerizing in Nodejs ecosystem
“why developers love docker?” can be assume as first question might asked about this article. Here let’s discuss about some cases but frankly need more profound technical session and more examples, so something is better than nothing.
- Variety in nodejs version and operating systems
- Sharing clean and well-tested environment between all technical team
- Some preceding affairs in associated to project for instance build, lint, install dependencies and more
- Optimizing project files running by utilizing custom command or flags
Mutlistage server side app
The below example has shown simple two phase dockerizing approch.
#
# _ _ _ _
#| | (_) | | |
#| |__ _ _ _| | __| |
#| '_ \| | | | | |/ _` |
#| |_) | |_| | | | (_| |
#|_.__/ \__,_|_|_|\__,_|
#
#
FROM node:18 as build
ENV NODE_ENV=development
WORKDIR /app
COPY package*.json ./
RUN npm --build-from-source install
COPY nest-cli.json package*.json tsconfig*.json tslint*.js /app/
COPY src /app/src
RUN npm run build
# _ _ _
# | | | | (_)
# _ __ _ __ ___ __| |_ _ ___| |_ _ ___ _ __
# | '_ \| '__/ _ \ / _` | | | |/ __| __| |/ _ \| '_ \
# | |_) | | | (_) | (_| | |_| | (__| |_| | (_) | | | |
# | .__/|_| \___/ \__,_|\__,_|\___|\__|_|\___/|_| |_|
# | |
# |_|
#
FROM node:18-alpine as production
ENV NODE_ENV=production
WORKDIR /app
COPY --from=build /app/dist/src /app/src
COPY --from=build /app/package.json /app/package.json
COPY --from=build /app/package-lock.json /app/package-lock.json
RUN npm install --production
EXPOSE 3000
CMD [ "node", "/app/src/main.js" ]
Separation between build and serve phase is major characteristic which had considered in the example. It would be merit if discussed about using optimized os in production stage that here is ALPINE version in linux distros.
After placing the Dockerfile in project, build process would be next step. The result should be identical as snapshot.
Wrap up and get hand dirty
All these words just tried to implies on MULTISTAGE benefits and recommend it. If you agree with me, unless you didn’t start your app, you can’t belive it.