Docker v17.05 introduced multi-stage builds giving us more ways to package and secure our applications. This post will show you how to use the power of the multi-stage builds for your projects. Check out the below Dockerfile and try to guess what we are trying to achieve here. We will go through each of the steps in detail right after that.
# Step 1 FROM node:10-alpine AS base WORKDIR /app COPY package.json . # Step 2 FROM base AS dependencies RUN npm install --only=production RUN cp -R node_modules prod_node_modules RUN npm install # Step 3 FROM dependencies AS build COPY . . RUN npm run build # Step 4 FROM base AS final COPY --from=dependencies /app/prod_node_modules ./node_modules COPY --from=build /app/dist ./dist COPY --from=build /app/entrypoint entrypoint RUN chmod +x entrypoint EXPOSE 3000 ENTRYPOINT [ "sh", "entrypoint" ]
Now let us go through the multiple stages we have used to create our final image -
node:10-alpine image and naming it as
base so that we can use it later in our build stage. This build stage will have only basic instructions. Here we have mentioned where our working directory will be an added our node projects
package.json file to the working directory.
We now take the
base image and start building our
dependencies image. Firstly we install only production dependencies then copy the node_modules directory to prod_node_modules directory. Then we install our dev dependencies on top of it. Step 4 will unravel why we did this.
We then take the
dependencies image and start building our
Now we pick only required directories for our final image. Which includes -
buildimage and giving it appropriate permissions.
The benefits of using multi-stage builds are -
Hope you learned something new today!
Use multi-stage builds - https://docs.docker.com/develop/develop-images/multistage-build