# Build locally
Instead of building images on your Kubernetes cluster with the Swisscom Application Platform it is also possible to do so locally on your machine without losing the power of Cloud Native Buildpacks (opens new window).
To do this we will use the pack
(opens new window) CLI.
# Install the pack-cli
To manually install the pack
CLI, you can visit the releases page on GitHub (opens new window).
- Download the
.tgz
or.zip
file for your platform - Extract the
pack
binary - (Optional) Add the directory containing
pack
toPATH
Or if you are on OSX you can use Homebrew to install pack
:
$ brew install buildpacks/tap/pack
# Build an image
Lets build a simple Java app into an image from source using the pack build
(opens new window) command:
# clone the source code repository
$ git clone https://github.com/buildpacks/samples
# go to the app directory
$ cd samples/apps/java-maven
# build the app source code
$ pack build myapp --builder cnbs/sample-builder:bionic
...
[builder] [INFO] ------------------------------------------------------------------------
[builder] [INFO] BUILD SUCCESS
[builder] [INFO] ------------------------------------------------------------------------
[builder] [INFO] Total time: 9.741 s
[builder] [INFO] Finished at: 2021-11-04T13:07:25Z
[builder] [INFO] ------------------------------------------------------------------------
===> EXPORTING
[exporter] Adding layer 'samples/java-maven:jdk'
[exporter] Adding 1/1 app layer(s)
[exporter] Adding layer 'launcher'
[exporter] Adding layer 'config'
[exporter] Adding layer 'process-types'
[exporter] Adding label 'io.buildpacks.lifecycle.metadata'
[exporter] Adding label 'io.buildpacks.build.metadata'
[exporter] Adding label 'io.buildpacks.project.metadata'
[exporter] Setting default process type 'web'
[exporter] Saving myapp...
[exporter] *** Images (44b4c3fbc9f7):
[exporter] myapp
That's it! You've now got a runnable app image called myapp
available on your local Docker daemon:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myapp latest 44b4c3fbc9f7 50 minutes ago 300MB
cnbs/sample-builder bionic 4f6d5965b82d 50 minutes ago 180MB
buildpacksio/lifecycle 0.11.3 c4e1a4508bb6 50 minutes ago 15.7MB
You can test your image locally if you want to by running it with Docker:
$ docker run --rm -p 8080:8080 myapp
|'-_ _-'| ____ _ _ _ _ _
| | | | _ \ (_)| | | | | | (_)
'-_|_-' | |_) | _ _ _ | | __| | _ __ __ _ ___ | | __ ___ _ ___
|'-_ _-'|'-_ _-'| | _ < | | | || || | / _` || '_ \ / _` | / __|| |/ // __| | | / _ \
| | | | | | |_) || |_| || || || (_| || |_) || (_| || (__ | < \__ \ _ | || (_) |
'-_|_-' '-_|_-' |____/ \__,_||_||_| \__,_|| .__/ \__,_| \___||_|\_\|___/(_)|_| \___/
| |
|_|
:: Built with Spring Boot :: 2.1.18.RELEASE
Check out the documentation (opens new window) on pack build
for further information.
# Push to a registry
Once you have built an image you can then push it into any container registry you want to from your local Docker daemon:
$ docker tag myapp:latest harbor.demo.kube-plus.cloud/demo/myapp
$ docker push harbor.demo.kube-plus.cloud/demo/myapp
Using default tag: latest
The push refers to repository [harbor.demo.kube-plus.cloud/demo/myapp]
83d85471d9f8: Pushed
61059a6d1032: Pushed
20e1cf6014bd: Pushed
ffed268ea11e: Pushed
a940b3489712: Pushed
5933667ff857: Pushed
444137a46fba: Pushed
6babb56be259: Pushed
latest: digest: sha256:b7bff5443e6bf48658308794b5a35a06760114b71bb506d002d30f8bb7bebf90 size: 1998
or directly with pack --publish
while building the image:
$ pack build harbor.demo.kube-plus.cloud/demo/myapp \
--builder paketobuildpacks/builder:base \
--publish
You should now be able to see the image in the on-cluster image registry: https://harbor.demo.kube-plus.cloud (opens new window)
# Builders / Buildpacks
The cnbs/sample-builder:bionic
builder that we used above is just an example of many builders capable of compiling your source code into a container image. It is recommended that you instead use one of the suggest builders below.
TIP: If you don't want to keep specifying a builder every time you build, you can set it as your default builder by running
pack config default-builder $BUILDER
.
Get a list of suggested builders:
$ pack builders suggest
Suggested builders:
Google: gcr.io/buildpacks/builder:v1 Ubuntu 18 base image with buildpacks for .NET, Go, Java, Node.js, and Python
Heroku: heroku/buildpacks:18 Base builder for Heroku-18 stack, based on ubuntu:18.04 base image
Heroku: heroku/buildpacks:20 Base builder for Heroku-20 stack, based on ubuntu:20.04 base image
Paketo Buildpacks: paketobuildpacks/builder:base Ubuntu bionic base image with buildpacks for Java, .NET Core, NodeJS, Go, Python, Ruby, NGINX and Procfile
Paketo Buildpacks: paketobuildpacks/builder:full Ubuntu bionic base image with buildpacks for Java, .NET Core, NodeJS, Go, Python, PHP, Ruby, Apache HTTPD, NGINX and Procfile
Paketo Buildpacks: paketobuildpacks/builder:tiny Tiny base image (bionic build image, distroless-like run image) with buildpacks for Java Native Image and Go
Tip: Learn more about a specific builder with:
pack builder inspect <builder-image>
When in doubt we suggest you to use
paketobuildpacks/builder:full
as--builder
. It is the most complete builder and supports most buildpacks and languages out of the box (Java, Ruby, PHP, Python, Golang, NodeJS, .NET Core, etc)
# Buildpack Documentation
Check out An App's Brief Journey from Source to Image (opens new window) for further information!