I previously talked about Knative Build and how it enables you to go from source code to a container image in a repository. You can write your Build from scratch or you can rely on many of the BuildTemplates Knative already provides. For example, in my Knative Tutorial, I show how to install Kaniko BuildTemplate and use Kaniko to build container images.
You normally need to write a Dockerfile
, so Knative Build (or Kaniko to be more precise) knows how to build the container image. Wouldn’t it be great if there was a an automatic way to build your app without having to define a Dockerfile
? Well, there is!
Cloud Native Buildpacks is a project that provides a higher-level abstraction for building apps compared to Dockerfiles. Cloud Native Buildpacks allow you to go from source code to a container image without having to define a Dockerfile
. It does this with some auto-detection magic to figure out what language your code is written in and what kind of dependencies it has. In the end, you end up with a runnable app image without having to worry about the details of authoring and optimizing a Dockerfile
.
The good news is that Knative has a Buildpacks template that makes it really easy to use Buildpacks in your Knative Build. I show how to do this in my Knative Buildpacks Build Template tutorial in detail but let’s recap here.
Install the Buildpacks template
First, you need to install the Buildpacks template:
kubectl apply -f https://raw.githubusercontent.com/knative/build-templates/master/buildpacks/cnb.yaml
And check that the template is installed:
kubectl get buildtemplate
NAME AGE
buildpacks-cnb 1m
Design the build
Next, let’s create a Build to build a sample Java app on GitHub (sample-java-app) using the build template. Create build.yaml
file:
apiVersion: build.knative.dev/v1alpha1
kind: Build
metadata:
name: buildtemplate-buildpack-sample-java-app-gcr
spec:
source:
git:
url: [https://github.com/buildpack/sample-java-app.git](https://github.com/buildpack/sample-java-app.git)
revision: master
template:
name: buildpacks-cnb
arguments:
- name: IMAGE
value: gcr.io/knative-atamel/sample-java-app:buildpack
One thing you’ll notice is that the sample-java-app
does not define a Dockerfile
. Buildpacks will use its auto-detection to build the image and Knative will push it to the location specified in IMAGE
argument (which is Google Container Registry in this case).
Run the build
We can finally run the build:
kubectl apply -f build.yaml
After a few minutes, check that the build is succeeded:
kubectl get build
NAME SUCCEEDED
buildtemplate-buildpack-sample-java-app-gcr True
At this point, you should see the image pushed to Google Container Registry:
Knative Build with Buildpacks is a great combination for building apps at a higher abstraction level compared to Dockerfiles!