pack for the journeyIn this tutorial, we’ll explain how to use pack and buildpacks to create a runnable app image from source code.
That means you’ll need to make sure you have pack installed:
NOTE:
packis only one implementation of the Cloud Native Buildpacks Platform Specification.
Before we set out, you’ll need to know the basics of buildpacks and how they work.
A buildpack is something you’ve probably leveraged without knowing, as they’re currently being used in many cloud platforms. A buildpack’s job is to gather everything your app needs to build and run, and it often does this job quickly and quietly.
That said, while buildpacks are often a behind-the-scenes detail, they are at the heart of transforming your source code into a runnable app image.
What enables buildpacks to go unnoticed is auto-detection. This happens when a platform sequentially
tests groups of buildpacks against your app’s source code. The first group that deems itself fit for your source code
will become the selected set of buildpacks for your app. Detection criteria is specific to each buildpack – for
instance, an NPM buildpack might look for a package.json, and a Go buildpack might look for Go source files.
Let’s see all this in action using pack build.
Run the following commands in a shell to clone and build this simple Java app.
# clone the repo
git clone https://github.com/buildpacks/samples
# go to the app directory
cd samples/apps/java-maven
# build the app
pack build myapp --builder cnbs/sample-builder:bionic
NOTE: This is your first time running
pack buildformyapp, so you’ll notice that the build might take longer than usual. Subsequent builds will take advantage of various forms of caching. If you’re curious, try runningpack build myappa second time to see the difference in build time.
That’s it! You’ve now got a runnable app image called myapp available on your local Docker daemon.
We did say this was a brief journey after all. Take note that your app was built without needing to install
a JDK, run Maven, or otherwise configure a build environment. pack and buildpacks took care of that for you.
To test out your new app image locally, you can run it with Docker:
docker run --rm -p 8080:8080 myapp
Now hit localhost:8080 in your favorite browser and take a minute to enjoy the view.
pack uses buildpacks to help you easily create OCI images that you can run just about anywhere. Try
deploying your new image to your favorite cloud!
In case you need it,
pack buildhas a handy flag called--publishthat will publish your app image to a Docker registry after building it.