Crafting multi-stage builds with Docker in Node.js

Docker has became an inevitable tool for development. Everyday developers face new challenges for containerizing their applications.One of the important problems are containerizing the application for different enviroments. Crafting multi-stage builds with Docker in Node.js.

you can ask me, why do we need multi-stage build in node.js applications.why can't we just build a single stage image and deploy it to server ?.

we are in a development era where Compiler and transpiler plays a vital role. especially, in javascript development environment. For Example, TypeScript and Babel.

Before multi-stage builds

Before the concept of multi-stage builds, application will have two Dockerfile. one is for development and another one is for production. this has been referred as a builder pattern. But maintaining two Dockerfiles is not ideal.

Dockerfile.dev

1 FROM node : 10.15.2 - alpine 2 WORKDIR /usr/src/app 3 COPY package.json ./ 4 RUN npm install 5 COPY . . 6 RUN npm run dev

Dockerfile

1 FROM node : 10.15.2 - alpine 2 WORKDIR /usr/src/app 3 COPY package.json ./ 4 RUN npm install 5 COPY /usr/src/app/dist ./dist 6 EXPOSE 4002 7 CMD npm start

Although, it solves the problem of development and production image builds. this will be expensive in the long run.it will take lots of spaces in local disk as well.

Using multi-stage builds

multi-stage build combines different environment Dockerfile into one to create a production build. For example, Staging build creates a compiled version of application source code and final build contains the compiled version deployed in the image container.

Let's see with an example of building muti-stage for Node.js with babel and MongoDB. Complete Source is available in this repository

Create a directory and initialize the Application with Express and Babel.

Staging Build

1 FROM node : 10.15.2 - alpine 2 WORKDIR /usr/src/app 3 COPY package.json ./ 4 COPY .babelrc ./ 5 RUN npm install 6 COPY ./src ./src 7 RUN npm run build

Above command, takes the node:10.15.2-alpine as base image and copies all the source code along with babel config. it builds the compiled code and stores it in dist folder in a container.

Final Build

1 FROM node : 10.15.2 - alpine 2 WORKDIR /usr/src/app 3 COPY package.json ./ 4 COPY .babelrc ./ 5 RUN npm install 6 COPY - - from=0 /usr/src/app/dist ./dist 7 EXPOSE 4002 8 CMD npm start

This command, takes the compiled version from previous staging build and stores it in a new image container. the magic happens in line COPY --from=0

COPY --from=0 line copies just the build artifacts from previous stage into the new stage.

Naming build stages

Instead of referring build stages with a Number, you can name them and use it for reference. For Example, we specify the stage build with the name appbuild

Staging Docker Build

1 FROM node : 10.15.2 - alpine AS appbuild 2 WORKDIR /usr/src/app 3 COPY package.json ./ 4 COPY .babelrc ./ 5 RUN npm install 6 COPY ./src ./src 7 RUN npm run build

Final docker Build

we refer the previous build with COPY --from=appbuild

1 FROM node : 10.15.2 - alpine 2 WORKDIR /usr/src/app 3 COPY package.json ./ 4 COPY .babelrc ./ 5 RUN npm install 6 COPY - - from=appbuild /usr/src/app/dist ./dist 7 EXPOSE 4002 8 CMD npm start

Complete Dockerfile

1 2 3 4 FROM node : 10.15.2 - alpine AS appbuild 5 WORKDIR /usr/src/app 6 COPY package.json ./ 7 COPY .babelrc ./ 8 RUN npm install 9 COPY ./src ./src 10 RUN npm run build 11 12 13 14 15 FROM node : 10.15.2 - alpine 16 WORKDIR /usr/src/app 17 COPY package.json ./ 18 COPY .babelrc ./ 19 RUN npm install 20 COPY - - from=appbuild /usr/src/app/dist ./dist 21 EXPOSE 4002 22 CMD npm start

Once, we complete the dockerfile. create docker compose to link multiple containers together.

Create a docker-compose.yml file and add the following,

1 version : "3" 2 3 services : 4 app : 5 container_name : app 6 restart : always 7 build : . 8 environment : 9 - PORT=4002 10 ports : 11 - "4002:4002" 12 links : 13 - mongo 14 mongo : 15 container_name : mongo 16 image : mongo 17 volumes : 18 - ./data : /data/db 19 ports : 20 - "27017:27017"

After that, Run the docker compose with the command,

1 docker-compose up

you can see the docker builds an intermediate image and use it to build a final one. Once it builds a final one,Docker deletes the intermediate images.

Complete Source code is available in this repository

To Read More