# Overview

Image Builder Build Container Images from Source Code

# What is kpack?

kpack (opens new window) is a Kubernetes native container build service, that allows you to build container images from your application source code.

It extends Kubernetes to provide builds of OCI images as a platform implementation of Cloud Native Buildpacks (CNB) (opens new window).

With Cloud Native Buildpacks it is possible build runnable container images directly from your source code without having to deal with Dockerfiles or figure out how to define container images yourself.

It provides a declarative builder resource that configures a Cloud Native Buildpacks build configuration with the desired buildpack order and operating system stack.

It provides a declarative image resource that builds an OCI image and schedules rebuilds on source code changes and/or from buildpack or system stack (base root filesystem) updates.

kpack gif

See kpack on github (opens new window) for further information.

# Quick Tutorial

  1. Apply a kpack image resource

    An image resource is the specification for an OCI image that kpack should build and manage for you.

    The example included here utilizes the Spring Pet Clinic sample app (opens new window). We encourage you to substitute it with your own application.

    Create an image resource:

    apiVersion: kpack.io/v1alpha2
    kind: Image
    metadata:
      name: tutorial-image
      namespace: default
    spec:
      tag: <DOCKER-IMAGE-TAG>
      serviceAccountName: tutorial-service-account
      builder:
        name: my-builder
        kind: Builder
      source:
        git:
          url: https://github.com/spring-projects/spring-petclinic
          revision: 82cb521d636b282340378d80a6307a08e3d4a4c4
    

    Apply that image resource to the cluster

    $ kubectl apply -f image.yaml
    

    You can now check the status of the image resource.

    $ kubectl -n default get images
    

    You should see that the image resource has an unknown READY status as it is currently building.

    NAME                  LATESTIMAGE   READY
    tutorial-image                      Unknown
    

    You can tail the logs for image that is currently building using the kp cli (opens new window)

    $ kp build logs tutorial-image -n default
    

    Once the image resource finishes building you can get the fully resolved built OCI image with kubectl get

    $ kubectl -n default get image tutorial-image
    

    The output should look something like this:

    NAMESPACE   NAME                  LATESTIMAGE                                        READY
    default     tutorial-image        index.docker.io/your-project/app@sha256:6744b...   True
    

    The latest OCI image is available to be used locally via docker pull and in a kubernetes deployment.

  2. Run the built app locally

    Download the latest OCI image build available from step #1 and run it with docker.

    $ docker run -p 8080:8080 <latest-image-with-digest>
    

    You should see the java app start up:

    
               |\      _,,,--,,_
              /,`.-'`'   ._  \-;;,_
     _______ __|,4-  ) )_   .;.(__`'-'__     ___ __    _ ___ _______
     |       | '---''(_/._)-'(_\_)   |   |   |   |  |  | |   |       |
     |    _  |    ___|_     _|       |   |   |   |   |_| |   |       | __ _ _
     |   |_| |   |___  |   | |       |   |   |   |       |   |       | \ \ \ \
     |    ___|    ___| |   | |      _|   |___|   |  _    |   |      _|  \ \ \ \
     |   |   |   |___  |   | |     |_|       |   | | |   |   |     |_    ) ) ) )
     |___|   |_______| |___| |_______|_______|___|_|  |__|___|_______|  / / / /
     ==================================================================/_/_/_/
    
     :: Built with Spring Boot :: 2.2.2.RELEASE
    
  3. kpack rebuilds

    We recommend updating the kpack image resource with a CI/CD tool when new commits are ready to be built.

    NOTE: You can also provide a branch or tag as the spec.git.revision and kpack will poll and rebuild on updates!

    You can simulate an update from a CI/CD tool by updating the spec.git.revision on the image resource used in the step #1.

    If you are using your own application please push an updated commit and use the new commit sha. If you are using Spring Pet Clinic you can update the revision to: 4e1f87407d80cdb4a5a293de89d62034fdcbb847.

    Edit the image resource with:

    $ kubectl -n default edit image tutorial-image
    

    You should see kpack schedule a new build by running:

    $ kubectl -n default get builds
    

    You should see a new build with:

    NAME                                IMAGE                                          SUCCEEDED
    tutorial-image-build-1-8mqkc       index.docker.io/your-name/app@sha256:6744b...   True
    tutorial-image-build-2-xsf2l                                                       Unknown
    

    You can again tail the logs for the image with the kp cli (opens new window).

    $ kp build logs tutorial-image -n default -b 2
    

    NOTE: This second build should be notably faster because the buildpacks are able to leverage cached data from the previous build.

Last Updated: 9/12/2022, 7:31:38 PM