How to create an Alpine-based JRE-only Docker image of Amazon Corretto?

1k views Asked by At

I will deploy my dockerized Java application to AWS so I would like to use Amazon Corretto. I also want to keep the size of the Docker image small so I want to use a JRE-only image based on Alpine.

Unfortunately Amazon does not provide a JRE-only image of Java 17 or Java 21 but I know I can create it using jlink so I tried this command

jlink --add-modules ALL-MODULE-PATH --output jre \
      --strip-debug --no-man-pages --no-header-files --compress=2

as suggested in this page. It works on my Windows PC but on Alpine it gives a strange error:

Error: java.io.IOException: Cannot run program "objcopy": error=2, No such file or directory

What does it mean? How can I create the base image?

1

There are 1 answers

2
Pino On BEST ANSWER

On Linux jlink needs a tool called objcopy that is not present in Alpine but can be installed with:

apk add --no-cache binutils

Also note that --compress=2 is deprecated in Java 21 after JDK-8293667. This fact is not reported yet in the official documentation but is present in the help page that says:

--compress <compress>         Compression to use in compressing resources:
                              Accepted values are:
                              zip-[0-9], where zip-0 provides no compression,
                              and zip-9 provides the best compression.
                              Default is zip-6.
                              Deprecated values to be removed in a future release:
                              0:  No compression. Equivalent to zip-0.
                              1:  Constant String Sharing
                              2:  Equivalent to zip-6.

so in Java 21+ you should use --compress=zip-6 or --compress=zip-9.

However I want to emphasize that an unofficial Alpine-based JRE-only image of Amazon Corretto already exists as amazoncorretto-jre so you could reduce your effort using it. It is much smaller than other official JRE images and I use it in production.