[Rust - Docker] Docker build and run Java Spring Boot Web app with multi-stage builds
Java Spring Boot Docker with multiple-stage builds
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
Multistage builds are useful to anyone who has struggled to optimize Dockerfiles while keeping them easy to read and maintain. See Use multi-stage builds | Docker Documentation - https://docs.docker.com/develop/develop-images/multistage-build/ to learn more.
Prerequites
Docker
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.
See Get Docker | Docker Documentation - https://docs.docker.com/get-docker/ to learn more.
Initialize
Use start.spring.io - https://start.spring.io/ to create a “col-java-docker-example” project. In the “Dependencies” dialog search for and add the “web” dependency as shown in the screenshot. Hit the “Generate” button, download the zip, and unpack it into a folder on your computer.
See Spring | Spring Quickstart Guide - https://spring.io/quickstart to learn more.
Mininal Image
Image | Size | Comment | |
---|---|---|---|
Builder | gradle:7.3.3-jdk17-alpine | ~560MB | [Gradle - Official Image |
Final | eclipse-temurin:17.0.1_12-jre-alpine | 47.47 MB~146MB | [Eclipse-temurin - Official Image |
Final | gcr.io/distroless/java17-debian11 | ~230MB | GoogleContainerTools/distroless: 🥑 Language focused docker images, minus the operating system. - https://github.com/GoogleContainerTools/distroless |
Final | openjdk:17.0.1-slim-bullseye | ~407MB | [Openjdk - Official Image |
Build
Builder Image
First, build the builder image with Dockerfile-base
file:
1 | # Dockerfile-base |
Build builder image:
1 | docker build . -f Dockerfile-base -t cloudolife/base-col-java-docker-example |
Final Image
Then, build the final image with Dockerfile-final
file:
1 | # Dockerfile-final |
Complete Dockerfile
1 |
|
Build builder image:
1 | docker build . -t cloudolife/base-col-java-docker-example --target Builder |
1 | # Dockerfile |
Build builder image:
1 | docker build . -t cloudolife/col-java-docker-example |
Run
Run the final image:
1 | $ docker run -it --rm --name col-java-docker-example -p 8080:8080 cloudolife/col-java-docker-example |
Open your browser to visit http://localhost:8080
.
Check
Check image size:
1 | docker images | grep java |
FAQs
org.apache.tomcat.jni.LibraryNotFoundError: Can’t load library: /app/bin/libtcnative-1.so, Can’t load library: /app/bin/liblibtcnative-1.so, no tcnative-1
1 | org.apache.tomcat.jni.LibraryNotFoundError: Can't load library: /app/bin/libtcnative-1.so, Can't load library: /app/bin/liblibtcnative-1.so, no tcnative-1 in java.library.path: /opt/java/openjdk/lib/server:/opt/java/openjdk/lib:/opt/java/openjdk/../lib:/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib, no libtcnative-1 in java.library.path: /opt/java/openjdk/lib/server:/opt/java/openjdk/lib:/opt/java/openjdk/../lib:/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib |
TODO.
References
[1] Spring | Spring Quickstart Guide - https://spring.io/quickstart
[2] Get Docker | Docker Documentation - https://docs.docker.com/get-docker/
[4] Dockerfile reference | Docker Documentation - https://docs.docker.com/engine/reference/builder/
[5] Gradle - Official Image | Docker Hub - https://hub.docker.com/_/gradle
[6] Openjdk - Official Image | Docker Hub - https://hub.docker.com/_/openjdk
[7] Tomcat - Official Image | Docker Hub - https://hub.docker.com/_/tomcat
[8] Eclipse-temurin - Official Image | Docker Hub - https://hub.docker.com/_/eclipse-temurin