lightweight Docker image for JDK11 micro-services
- lightweight (~53Mb + your jar)
- uses free JDK11 distribution from Amazon Corretto 11.0.13
- prebuilt image on Dockerhub or build your own (see Dockerfile)
- s2i image: Dockerhub, Dockerfile
- integration with Openshift
Given your application jar is named my-cool-app.jar
, create a Dockerfile with the following contents:
FROM maslick/minimalka:jdk11
WORKDIR /app
EXPOSE 8080
COPY my-cool-app.jar ./app.jar
CMD java $JAVA_OPTIONS -jar app.jar
docker build -t my-cool-app .
docker run -d my-cool-app:latest
git clone https://github.com/maslick/minimalka.git && cd minimalka
vim Dockerfile
docker build -t my-minimalka:jdk11 .
FROM my-minimalka:jdk11
WORKDIR /app
EXPOSE 8080
COPY my-cool-app.jar ./app.jar
CMD java $JAVA_OPTIONS -jar app.jar
docker build -t my-cool-app .
docker run -d my-cool-app:latest
Install Corretto 11 JDK and use jenv to switch to it in your shell:
$ jenv add /Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home
$ jenv shell corretto64-11.0.13
$ java -version
openjdk version "11.0.13" 2021-10-19 LTS
OpenJDK Runtime Environment Corretto-11.0.13.8.1 (build 11.0.13+8-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.13.8.1 (build 11.0.13+8-LTS, mixed mode)
git clone https://github.com/maslick/minimalka.git
cd minimalka/demo
./gradlew dockerBuild
docker run -d -p 8080:8081 -e JAVA_OPTIONS=-Dserver.port=8081 minimalka-boot
open http://localhost:8080/helloworld
This s2i image supports binary builds only. Meaning that the build stage doesn't build your jars, instead you provide already built artifacts. Minimalka s2i image will just check and copy your binaries into the resulting lightweight image.
s2i build . maslick/minimalka-s2i mycoolapp:latest
More or less it works in the same way as the Docker multistage builds. The resulting image is production ready and does not have the root privilage escalation flaw.
In a typical use-case you would have a Jenkins pipeline with several stages: checkout
, build
, test
, Build Docker image
, Deploy to dev
. During the Build Docker image
stage you inject the already built jar (build stage) into the build config
and start the build.
oc new-build --name minimalka-runtime --docker-image maslick/minimalka-s2i --binary=true
oc start-build minimalka-runtime --from-file my-cool-app.jar
oc new-app minimalka-runtime --name my-cool-app
oc expose svc my-cool-app