Docker build problems

Docker is a great tool for deploying scalable web applications for a large variety of technologies ranging from Node.js, Python, MySQL, and more.  We have been using Docker at Computer Know How for some time but we recently ran into a challenge with an application we were setting up for Docker.

The application was written in Vue.js so we wanted to deploy the application using the NGINX Alpine image.  However, we needed to have Node.js installed to build the static files from the Vue.js source files.  We looked into some options like Scott Mebberson’s alpine-nginx-nodejs Docker image, but that image used Node.js 6 and our application required Node.js 8 to build properly.  That forced us to look for another option.

I knew that there must be a better way to pre-build the Vue.js application and then deploy it using the NGINX Alpine Docker image, so I did some research and found the perfect solution: multi-stage builds.

Multi-stage builds

Multi-stage builds allow you to use intermediate images to build the application piece by piece until you construct the final image where you run the application.  With this information, the build became quite simple and required only two stages:

  1. Build the Vue.js application to create the distributable files
  2. Serve the distributable files using an NGINX web server

Using the multi-stage build documentation I constructed a Dockerfile to perform the two stages required for building this application.

It worked perfectly, and we were able to deploy the application.  Multi-stage builds also have the advantage of creating smaller containers because intermediate images and files are not kept unless they are copied to the final stage of the build.