Using Buildpacks to provision OCI-compliant container images
Tags: docker
Published: 2020-04-27 14:59:26

It never fails that the CNCF seem to always be cooking up something interesting in their ecosystem. In my free time, I always seem to find myself in a habit of playing in the Sandbox to see what new cutting edge tools I can add to my collection. It is my goal today to introduce you to a project at the Sandbox stage known as "Buildpacks".

#What are Buildpacks?#

Buildpacks are an OCI-compliant tool for building applications that serve as a higher-level abstraction as opposed to writing Dockerfiles. The project was spawned by Pivotal and Heroku in 2011 and joined the [Cloud Native Sandbox](https://www.cncf.io/) in October 2018. Since then, Buildpacks has been adopted by Cloud Foundry and other PaaS such as Gitlab, Knative, Deis, Dokku, and Drie. 

The project seeks to unify the buildpack ecosystems with a [platform-to-buildpack contract](https://github.com/buildpacks/spec/blob/master/buildpack.md) that is well-defined and that incorporates years of learning from maintaining production-grade buildpacks at both Pivotal and Heroku.

#Installing pack#

In this demo, we're going to learn how to use `pack` and **buildpacks** to create a runnable app image from source code.

###Prerequisites###
* Docker

You can install the most recent version of `pack` (version 0.6.0) as an executable binary on the following operating systems:
* [macOS](https://buildpacks.io/docs/install-pack/#macos)
* [Linux](https://buildpacks.io/docs/install-pack/#linux)
* [Windows](https://buildpacks.io/docs/install-pack/#windows)

###[macOS](https://buildpacks.io/docs/install-pack/#macos)###

To install `pack` on macOS, the easiest way is to use Homebrew:

```
brew tap buildpack/tap
brew install pack
```

###[Linux](https://buildpacks.io/docs/install-pack/#linux)###

```
wget https://github.com/buildpacks/pack/releases/download/v0.6.0/pack-v0.6.0-linux.tgz
tar xvf pack-v0.6.0-linux.tgz
rm pack-v0.6.0-linux.tgz
./pack --help
```

From there, you can copy the executable to a directory like `/usr/local/bin` or add the current directory to your `PATH`.

###[Windows](https://buildpacks.io/docs/install-pack/#windows)###

You can install the Windows executable `pack` by downloading the Windows [ZIP file](https://github.com/buildpacks/pack/releases/download/v0.6.0/pack-v0.6.0-windows.zip).

#Building an app#

Building an app using Cloud Native Buildpacks is very straightforward. 

###[Select a builder](https://buildpacks.io/docs/app-developer-guide/build-an-app/#1-select-a-builder)###

To [build](https://buildpacks.io/docs/concepts/operations/build/) an app you must first decide what [builder](https://buildpacks.io/docs/concepts/components/builder/) you are going to use. A builder includes the [buildpacks](https://buildpacks.io/docs/concepts/components/buildpack/) that will be used as well as the environment for building and running your app.

When using the `pack`, you can run `pack suggest-builders` for a list of suggested builders.

```
pack suggest-builders
```

For this tutorial, we’re actually going to use a sample builder, `cnbs/sample-builder:bionic`, which is not listed as a suggested builder for good reason. It’s a sample.

###[Build your app](https://buildpacks.io/docs/app-developer-guide/build-an-app/#2-build-your-app)###

Now that you’ve decided on what builder to use, we can build our app. For this example, we will use our [samples](https://github.com/buildpacks/samples) repo for simplicity.

```
# clone the repo
git clone https://github.com/buildpacks/samples

# build the app
pack build sample-app --path samples/apps/java-maven/ --builder cnbs/sample-builder:bionic
```

**Tip**: If you didn’t want to keep specifying a builder every time you build, you could set it as your default builder by running `pack set-default-builder <BUILDER>`.

###[Run it](https://buildpacks.io/docs/app-developer-guide/build-an-app/#3-run-it)###

```
docker run --rm -p 8080:8080 sample-app
```

**Congratulations!**

The app should now be running and accessible via [localhost:8080.](localhost:8080)