Lean, Mean, Java Virtual Machine: Making Your Docker 7x Lighter With Alpine Linux
Here's how to make an awesome JVM, by lightening your Docker setup with Alpine Linux. Learn about building a minimal JVM container, what Alpine Linux actually is, and whether or not image size matters.
Join the DZone community and get the full member experience.Join For Free
there has definitely been an awakening of some kind. lately, we’ve been hearing more and more about alpine linux, a lightweight distro that’s rising in popularity mostly because of its docker use case. a single takipi support chat a few months back piqued our interest, and since then we see more and more of our users trying it out with their containers. in this post, we’ll do a quick overview and walkthrough of alpine and how you can use it to build lightweight docker containers with java or any other jvm language.
let’s get started.
— takipi (@takipid) january 27, 2016
so what’s alpine linux anyway?
alpine linux is an independent stripped down the flavour of linux that’s based on musl libc (pronounce ‘muscle’) and busybox . apart from being lightweight, it’s also security oriented and uses grsec/pax which is a security enhancement to the linux kernel. simply having fewer packages and utilities that are otherwise added by default in other distributions also adds to its security.
in addition, alpine uses its own package manager called apk which is fast and pretty straightforward to use. and another cool perk is its run-from-ram feature.
alpine linux on the rise – google trends
does image size really matter?
docker is all about minimalism but when it comes down to jvms on docker, the official java docker image would add up to over 800mb. this is a pain even if you’re not disk space-sensitive because of two main reasons.
the bigger the image size, the longer it takes to:
1. build it
2. ship it over the network
when you’re dealing with tens and hundreds of containers, or constantly shipping updates, it enhances the negative effects of big image sizes so, yes, it matters. at the same time, an alpine base image would only take 5mb of space.
compared to ubuntu which is probably the most popular distro for docker users, that’s over a 37x improvement, saving up 183mb of disk space. with that said, alpine is not for everyone and requires some digging around depending on your specific use case and the kind of features you’re expecting to get from your os. if being clean and minimal is one of your biggest concerns, then by all means, you should check it out.
alpine vs ubuntu official docker images : 1.1m pulls on alpine and 39.3m on ubuntu
super useful on java
while not specific to java use cases, using alpine provides a huge size benefit when used together with a jvm on docker. the out-of-the-box official java docker image takes over 800mb, with alpine, it can go down to 117mb if you’re only including the jre on java 8. that’s almost 7 times lighter! as for the actual installation, it depends on which version of java you’re going for. since alpine uses musl libc, we have 2 options:
1. openjdk installations work out-off-the-box with musl libc,
here’s an example
2. oracle hotspot depends on glibc and requires an additional step.
the most popular dockerfiles for oracle java were published by frolvlad (9.3k pulls) and anapsix (6.9k pulls), including different configurations for minimal installations including glibc. here’s one way to add glibc on alpine. on a sidenote, a nice little trick is viewing docker images through imagelayers.io. here’s how the java 8 image from anapsix looks like:
how to install takipi on alpine linux?
we first heard of alpine following requests from users who wanted to monitor their jvms with takipi . to be able to run takipi on alpine we had to include a few more dependencies. apart from glibc, we also require the zlib compression library, libstdc++ and the low-level runtime library libgcc . hold on tight! attached is a dockerfile that describes all the required steps with comments. you’ll see how to install java on alpine with glibc, add the additional dependencies, install takipi, and monitor a simple scala server which has a few errors (btw, to learn more about what is takipi, you can check out this page ):
to dig in deeper into alpine and get involved in its development you can check out their irc channels on freenode : #alpine-linux and #alpine-devel where the core developers hang out. you can also join the mailing lists and forums .
from the comments we’re seeing on twitter, hn, and reddit, the general sentiment is very positive and many alpine users are seeing significant image size reductions, without compromising on other os features. the numbers don’t lie, it’s gaining some serious momentum.
the benefits of using a full blown distribution sometimes just aren’t worth it when compared to a minimal os like alpine. especially for the container use case. you might even call them… micro containers.
we hope that this post helped you discover something new, and would be glad to hear your thoughts about it. please feel free to use the comments section below.
Published at DZone with permission of Alex Zhitnitsky, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.