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?
On Linux
jlink
needs a tool calledobjcopy
that is not present in Alpine but can be installed with: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: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.