Kotlin Native in UBI

2020-05-20

Does Kotlin Native compile and run on RedHat's Universal Base Image (UBI)?

First, why build your container image on top of UBI at all? Well, it is apparently best practice on OpenShift, at least if you ask RedHat. They can explain better.

Since Kotlin Native does not work in quite any container  — to my knowledge, Alpine is currently not supported — the question presents itself: will it run?

Turns out it does in the way you would expect:

Dockerfile
FROM registry.access.redhat.com/ubi7/ubi:latest AS build

<install Java and Kotlin Native>

COPY hello-world.kt ./
RUN export PATH=kotlin-native-linux-${kotlin_version}/bin:$PATH \
 && kotlinc-native hello-world.kt -o hello-world

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

FROM registry.access.redhat.com/ubi7/ubi-minimal:latest AS run

COPY --from=build hello-world.kexe ./hello-world

CMD ./hello-world
Note

I skipped the installation of Kotlin Native here; it is not as neat as one would wish. Maybe there will be a yum package at some point.

Full sources: Gist

The final image is quite small (~80MB), with only a single binary added to the base image — which is, of course, the point of the exercise.

Proof of life:

$ docker build -t ubi-knative-demo . \
    && docker run --rm ubi-knative-demo
<snip>
Hello World, I'm Kotlin Native!
Caution

Building the build stage takes a while (~4min for me) and the resulting image is huge (~2.9GB). Docker does not cache and clean up intermediate images in multi-stage builds very well, so exercise caution lest you bury your system drive (like I did).

docker image prune is your friend.

DockerContainersKotlinDevOpsOpenShift

Hacking Cucumber: Isolating Features With Docker

Authorizing Only Your Team With Ansible