Buildpacks : Transform Application Source Code to OCI images without Docker Files

Posted in Recipe on October 26, 2022 by Venkatesh S ‐ 3 min read


Introduction to Cloud Native Buildpacks

It is a common practice for all of us to write our own docker files to create containers of our applications. May be sometimes you might have wondered how the PAAS platforms like heroku, openshift, VMware Tanzu and many others containerize the applications into container images without you having to explictly do anything. If you want to do the similar magic, this post will provide you with some insights.

Welcome to the magical world of buildpacks. Buildpacks were first conceived by Heroku in 2011. Since then, they have been adopted by Cloud Foundry and other PaaS such as Google App Engine, Gitlab, Knative, Deis, Dokku, and Drie.

With buildpacks, you can transform your application source code into container images directly that can run on any cloud. You don’t need to write any docker files explicitly. All you need is your source code. The tool automatically and magically transforms your source code into cloud native OCI container image that can later be deployed on any cloud platforms or run locally using docker. This is as shown in the representation below.

intro

This post begins with an end. All you need is just 2 steps.

  • Ensure that you have the source code for your application.
  • Build the container images using buildpacks with just “pack” command. This will containerize your source code.
    pack build my-project  --builder packetobuildpacks/builder:full
    

Why Cloud Native Buildpacks?

  • Control - Balanced control between App Devs and Operators.
  • Compliance - Ensure apps meet security and compliance requirements.
  • Maintainability - Perform upgrades with minimal effort and intervention.

Features of the platform

Here are some of the significant features of this platform.

  • Auto detection - Images can be built directly from application source without additional instructions.
  • Bill-of-Materials (BOM) - Insights into the contents of the app image through standard build-time SBOMs in CycloneDX, SPDX and Syft JSON formats.
  • Multi-language Support - Supports more than one programming language family.
  • Minimal App Image - Image contains only what is necessary. Best practices are followed.
  • Rebasing Support - Instant updates of base images without re-building.
  • Reusability - Leverage production-ready buildpacks maintained by the community.
  • Reproducibility - Reproduces the same app image digest by re-running the build.
  • Modular / Pluggable - Multiple buildpacks can be used to create an app image.
  • Advanced Caching - Robust caching is used to improve performance.

Installation

Installing pack is simple.


# on Mac
brew install buildpacks/tap/pack

# on linux (ubuntu)
sudo add-apt-repository ppa:cncf-buildpacks/pack-cli
sudo apt-get update
sudo apt-get install pack-cli

# install using a simple shell script
(curl -sSL "https://github.com/buildpacks/pack/releases/download/v0.27.0/pack-v0.27.0-linux.tgz" | sudo tar -C /usr/local/bin/ --no-same-owner -xzv pack)

# install on windows
choco install pack --version=0.27.0

Refer the pack installation section for more details.

How does buildpacks work?

how

Buildpack comprises of two phases.

  • Detect phase The detect phase runs against your source code to determine if the buildpack is applicable or not. Once a buildpack is detected to be applicable, it proceeds to the build stage. If the project fails detection the build stage for a specific buildpack is skipped.

  • Build phase The build phase runs against your source code to set up the build-time and run-time environment. It downloads dependencies and compile your source code (if needed). It also sets appropriate entry point and startup scripts

builder

Builders are an ordered combination of buildpacks with a base build image, a lifecycle, and reference to a run image. They take in your app source code and build the output app image. The build image provides the base environment for the builder (for eg. an Ubuntu Bionic OS image with build tooling) and a run image provides the base environment for the app image during runtime. A combination of a build image and a run image is called a stack.

References

Official documentation https://buildpacks.io/docs/