Streamlining Docker Image Creation with Cloud Native Buildpacks
The Inefficiencies of Traditional Docker Image Building
When building a Docker image, it’s easy to unnecessarily download thousands of packages, wasting time, bandwidth, and money. This is because each command in the Dockerfile creates a new layer, and these layers are reused only if the instructions and external files don’t change. A simple change to the application source code can force Docker to rebuild all layers, resulting in redundant dependency downloads.
Introducing Cloud Native Buildpacks
Cloud Native Buildpacks emerged as a convenient way to build Docker images by leveraging the decade of experience large hosting providers have in generating and hosting Docker images. By capturing best practices, Buildpacks ensure quick and efficient Docker image builds, often with no additional configuration.
Building a Simple Node.js Application
To demonstrate the power of Buildpacks, let’s build a simple Node.js application using the Express application generator. The sample application displays a “Welcome to Express” web page and can be found on GitHub. Although simple, this application illustrates how to build a Docker image hosting a Node.js application and some of the inefficiencies that a naive approach to building Docker images can lead to.
Tutorial Prerequisites
To follow along, you’ll need:
- Node.js: provides downloads and instructions on their website
- Docker: provides downloads and instructions on their website
- pack CLI tool: available from the Buildpacks website
Building a Docker Image with Buildpacks
With Buildpacks, you don’t need a Dockerfile. Simply run the pack command, and it will detect that the application is written against Node.js, install all dependencies, and produce the Docker image.
pack build my-node-app --builder paketobuildpacks/builder:base
This approach is more efficient because Buildpacks use Docker volumes to persist files like dependencies between builds, making it possible to reuse existing dependencies even when the package.json file changes.
The Benefits of Buildpacks
Buildpacks abstract away much of the knowledge required to build a high-quality Docker image. With Buildpacks, you can Dockerize your Node.js applications without worrying about hacking a Dockerfile to prevent unnecessary dependency downloads. This approach makes it possible to build a Docker image with a single call to pack, ensuring quick and efficient builds.