3

A bit background.

My goal is to create a super slim docker image with JRE on ARM64. Here are possible areas where I can affect on the final image size.

  1. Using JAVA 9 and up allows me to leverage new java modules capability.
  2. Using Alpine for ARM64 provides a super slim base image.

What I have done so far: There is OpenJDK 9 and up releases for ARM64, using modules capability I got JRE size around 30M, that's a great achievement.

Now I'm working on moving to the Alpine base image, it requires to have an OpenJDK be compiled with musl-libc. I managed to recompile the lasted OpenJDK with glibc, my question is how it's complicated to compile the OpenJDK 9 with musl libc, my understanding says, the glibc and musl expose the same interface, basically, the OpenJDK should be compatible with musl-libc.

Any directions to hit this challenge will be appreciated.

Denis Voloshin
  • 768
  • 10
  • 32
  • OpenJDK is the official Java implemention of Alpine and supports musl, so building for musl on Alpine should be as simple. You could get started at the Portola homepage: https://openjdk.java.net/projects/portola. The experimental jdk9 fork is here: https://hg.openjdk.java.net/portola/jdk9 – valiano Oct 14 '18 at 14:15
  • Note that, only OpenJDK 7 and 8 were GA'd on Alpine; then there was 11 early access build, which is gone now. 9 and 10 were not released. So while you could try building the Portola branches by yourself, you may run into functional issues of the implementation itself. That's the main problem, IMO. – valiano Oct 14 '18 at 14:19
  • Actually, OpenJDK 12 early access became available just 3 days ago (for x64: http://jdk.java.net/12), so it seems under active development - you may consider that, instead of 9. Chances are, it would be in better shape, and may see fixes towards an official 12 release. – valiano Oct 14 '18 at 14:26
  • Valiano, I really appreciate your tips. The OpenJDK 11, you mentioned, worked for me for x86 but I didn't find The OpenJDK 11 release for Alpine on ARM64. – Denis Voloshin Oct 14 '18 at 17:17
  • Sure, Denis. Unfortunately I estimate that the early access builds were only made available for x64. I couldn't find any ready made ARM64 musl builds - closest I came are ARM32 builds on Aleksey Shipilëv's CI site: https://builds.shipilev.net – valiano Oct 14 '18 at 18:00
  • Though, if you have OpenJDK 9 already built for glibc, perhaps a good step forward will be adding glibc support to your Alpine image. That way you could get your image working with minimal effort. Adding glibc support to Alpine images is pretty common and should be quite straight forward. For example, take a look at: https://stackoverflow.com/a/38433396/7256341. glibc will have some overhead over musl, but I think it shouldn't be too significant – valiano Oct 14 '18 at 18:05
  • You can find ready to install ARM64 glibc .apk packages here: https://github.com/owlab-exp/alpine-pkg-glibc-arm64/releases – valiano Oct 14 '18 at 18:10
  • Cool, the glibc on must Alpine is the current task I'm struggling with, thanks for more details and tips, hopefully the adding glibc on Alpine doesn't blow it up to 50M like debian slim image, what I'm using now ,Thanks you very much and have a nice day – Denis Voloshin Oct 15 '18 at 06:31
  • BTW, what do you think, the idea to take the sources of OpenJDK 12 for Alpine Linux / x64 and try to compile it on ARM64 by myself is it seems like a feasible task, for someone who came from Java world and the last time touched C/C++ was 13 years ago :) – Denis Voloshin Oct 15 '18 at 06:43
  • Building OpenJDK shouldn't be difficult - give it a try :-) Good luck! – valiano Oct 15 '18 at 07:00

1 Answers1

1

Just in (May/19) - openjdk9 (experimental) for AArch64 is now available from Alpine repositories!

Package details: https://pkgs.alpinelinux.org/package/edge/testing/aarch64/openjdk9

Grab it using:

apk add openjdk9 --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing

The other day I had to build OpenJDK from source on musl Alpine 3.8. Luckily, it was quite smooth! So, if you'll end up having to build OpenJDK from source for ARM64 musl support, you could follow these steps with some modifications for your target.

Note 1: To extend the discussion in comments, OpenJDK on musl Alpine is currently work in progress. The latest available is JDK 12 early access, for X64 only: http://jdk.java.net/12. JDK 9, 10 and 11 have development branches, but were not GA'd and officially released. Depending on your usage of the built OpenJDK, this should be seriously considered.

Naturally, it's always better to use ready and tested OpenJDK binaries, if such are available.

Note 2: The below steps were tested with OpenJDK 11, over Alpine 3.8 traditional disk installation (not docker), on a X86 workstation.

Prerequisites:

Packages

The following packages should be installed prior to build (hope I'm not missing any):

// build tools and utilities  
apk add build-base autoconf bash coreutils gawk grep mercurial zip  
// X11 libraries   
apk add libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev  
// Additional libraries  
apk add alsa-lib-dev cups-dev fontconfig-dev

Boot JDK

Then, ironically, you need a boot JDK: OpenJDK of the same of previous version installed... It could be OpenJDK that runs on the host (not cross compiled), so you should get your hands on OpenJDK 10 or 11 musl X86 builds.

At some point, an OpenJDK 11 early access binary was made available, but it was removed from the OpenJDK download page, since it was not production ready. For JDK bootstrapping, it may be enough, so try getting a hold of it: openjdk-11+28_linux-x64-musl_bin.tar.gz. If you want (or have to) stick to JDK9, then OpenJDK 8 could be used for bootstrapping, and no problem there since it's available as the openjdk8 apk package.

Usually the boot JDK is automatically picked by the build, but if not, you could specify it to configure with --with-boot-jdk=[path-to-jdk].

Sources:

For Alpine OpenJDK builds, you should use OpenJDK's Portola branches. These are already compatible with musl. There are branches for JDK 9, 10 and 11. As noted above, they are experimental.

For grabbing the OpenJDK 11 source code:

hg clone https://hg.openjdk.java.net/portola/jdk11

Building:

Once you had everything set up, build is quite simple. I used:

cd jdk11
bash configure
make JOBS=8 LOG=info hotspot

References:

For further reference, see the JDK11 build instructions (not for Alpine specifically):

OpenJDK cross compiling:

valiano
  • 16,433
  • 7
  • 64
  • 79
  • Thank you very much for sharing your experience. Compile Portola source was my favorite direction so far, the only concern was before I will start the build process, The matter of the Boot JDK, as I said my target arch is ARM64, I not sure that I can find any available JDK with musl for ARM64 prior to JDK 9. According to your instructions and as I was thinking it has to be both. Am I correct or there is some trick. Is there a way to complie JDK for ARM64 on x86? – Denis Voloshin Oct 17 '18 at 10:32
  • @DenisVolohsin Sure, compiling for ARM64 on x86 is actually the preferred approach - it's called cross compiling: the compiler and build tools are instructed to produce code and binaries for the specified target. This requires support by the tool chain and the project build system. In OpenJDK, configuring the build with `--openjdk-target=arm-linux-gnueabihf` will produce ARM OpenJDK binaries. There are detailed cross building instructions on the OpenJDK build page: http://cr.openjdk.java.net/~ihse/demo-new-build-readme/common/doc/building.html#cross-compiling – valiano Oct 17 '18 at 11:13
  • Great, thanks. Now I'm ready to start my adventure to crusade the OpenJDK build :) – Denis Voloshin Oct 17 '18 at 11:28
  • @DenisVolohsin Godspeed! – valiano Oct 17 '18 at 11:29
  • @DenisVoloshin It's been a while, but you may be interested to know that `openjdk9` and `openjdk10` are now available from Alpine repositories (`edge/testing`). I added the details to my answer. HTH! – valiano May 20 '19 at 06:29