Let's talk about Buildpacks

Buildpack - Build images from application source code

Buildpack is a concept of turning application source code into Docker images which can be run from anywhere.

One of the most frustrating things about Docker deployment is that we have to create our own Dockerfile or use others' Dockerfile. Those Dockerfile tell what an application needs to install in order to run.

For example,  a Node.js application might need something like this

FROM node:16-alpine

RUN mkdir -p /opt/app

WORKDIR /opt/app

COPY package*.json ./

ENV CI true

ENV NODE_ENV production

RUN npm ci

COPY . .

RUN chown -R node /opt/app

USER node

EXPOSE 8080

CMD [ "node", "main.js" ]
copied from dockerfile.netlify.app/node-16

This Dockerfile snippet instruct how an application should run such as

  • Operating System: Linux Alpine
  • Runtime: Node.js  16
  • Package Manager: npm

These is nothing with writing our own Dockerfile however we can improve this by using buildpacks. If you're familiar with Netlify or Vercel, you might have already seen how they automate the build process of the website. By using buildpacks, we only need to worry about our application source.

Problems with Dockerfile

Maintaining Dockerfile in many projects can be difficult when we want to make changes to all of them. For example, we need to upgrade dependency version or runtime version. We have to manually make changes to all those Dockerfile inside thoe projects. With buildpacks, we only do it once.

Picking the right environments

An application requires specific runtime version or environment to be running. For example, an application might need node-14  and a right package manger to run, so buildpacks need to know where to parse those which in this case it will try to find those environment inside the application source files such as .nvmrc or package.json

Open-source Buildpacks

There are several open source buildpacks I found so far.

Netlify/build-image

This is what netlify uses to build our websites.

GitHub - netlify/build-image: This is the build image used for running automated builds
This is the build image used for running automated builds - GitHub - netlify/build-image: This is the build image used for running automated builds

Nixpacks from Railway

GitHub - railwayapp/nixpacks: App source + Nix packages + Docker = Image
App source + Nix packages + Docker = Image. Contribute to railwayapp/nixpacks development by creating an account on GitHub.

Paketo Buildpacks

Paketo Buildpacks

pkg from Vercel

Not a buildpack, but it achieve almost the same purpose.

GitHub - vercel/pkg: Package your Node.js project into an executable
Package your Node.js project into an executable. Contribute to vercel/pkg development by creating an account on GitHub.

In conclusion

I find buildpacks interesting and innovative so I write this article to share it with some of you. If there's any mistakes I made, please let me know via my email: seanghay.dev@gmail.com

Thanks for reading! ✌️