Hi there! I’m playing around with Kotlin Native, o...
# kotlin-native
m
Hi there! I’m playing around with Kotlin Native, out of curiosity how small we can make Docker images with a single Kotlin binary. So far, I’m down to 64.6MB with an
ubuntu
base image:
Copy code
FROM ubuntu
COPY hello_world_linux64 /hello_world
ENTRYPOINT ["/hello_world"]
But 64.2MB of those is Ubuntu itself. Is there an option for the kotlinc-native compiler to statically link all libraries? Because if I build an image from scratch, like this:
Copy code
FROM scratch
COPY hello_world_linux64 /hello_world
ENTRYPOINT ["/hello_world"]
this will print this error at runtime:
Copy code
standard_init_linux.go:211: exec user process caused "no such file or directory"
And by the way: merry christmas / happy holidays to everyone 😉 🎄🎁
d
At that point you'd have to bundle up the entire OS in the binary, which doesn't give you anything.
👍 2
n
The biggest issue with Kotlin Native in Docker is that Kotlin Native doesn't support Musl C, therefore Alpine Linux (significantly smaller than Ubuntu) can't be used.
Mark - Would be interested to know how you created the Docker image. It is much smaller than the Debian one used in the weather_function sample ( https://github.com/JetBrains/kotlin-native/tree/master/samples/weather_function ).
m
@Dominaezzz yep, it would require all the needed native code from the OS, that’s exactly what I wanted here. But the binary doesn’t use everything from the OS, and I assume it doesn’t really need everything from the linked libraries as well. I only wondered if there is an option to create a single, self-contained binary. It seems Go is able to do that - I wonder if there’s an option for kotlinc-native as well.
@napperley I’m actually writing a blog post at the moment that describes exactly how I did that. 😊 As for the
weather_function
sample, I would think that that gets larger to due more required dependencies. I haven’t looked into it more to confirm.
d
You can make kotlinc-native statically link stuff in by using
-include-binary
to include static libraries. When included this way, they automatically link all symbols when building the final binary. So that's an option on Linux.
👍 1
m
Oh, that’s great! I will include you in my blog post and try this out at a later time 😊
@napperley if you’re interested, I just published the blog post: https://dev.to/mreichelt/christmas-hacking-squeezing-kotlin-native-into-docker-6ao
👍 1
n
The`-include-binary` flag only works for klibs unless that has changed recently.
d
Yeah, isn't that all you need it for?