Examples for presentation 'Docker (not only) for Java Developer'
Figlet is a program that generates text banners.
Dockerfile for alpine-figlet
:
FROM alpine
RUN apk add figlet --no-cache
ENTRYPOINT ["figlet"]
CMD ["Hello World!"]
To build save into Dockerfile
and run docker build . -t alpine figlet
.
FROM
– starts from alpine image (with latest) tagRUN
– installs figletENTRYPOINT
– set default executable to figlet.CMD
– set default parameters passed to figlet to Hello World
Running docker run alpine-figlet
shows default "Hello World!" message. Different params can be passed
and will replace the CMD
- e.g. docker run alpine-figlet I like trains!
will print "I like trains".
We can still replace the entrypoint and run shell: docker run -it --entrypoint "/bin/sh" alpine-figlet
.
To build:
docker build . -t temurin-containeraware
To run with CPU/memory limits:
docker run -it --rm --memory 300m --memory-swap 300m --cpu-period 100000 --cpu-quota 200000 temurin-containeraware
Inside run built jar with:
java ShowMxBeanCpuAndMemoryInfo
We can combine it with -XshowSettings:system
or set explicit number of processors: -XX:ActiveProcessorCount=4
.
Simple kind-of todos app that allows to add and remove entries (todos):
- after adding the todo has
done = false
. - after marking as done the todo has
done = true
. - todo with
done = true
is in done section and can be deleted.
By default app is using port 7070
and can be available at http://localhost:7070/
.
Used libraries:
- Javalin - https://javalin.io/
- Jte - https://jte.gg/
- MongoDB sync driver - https://www.mongodb.com/docs/languages/java/
- Bulma CSS - https://bulma.io/
- JUnit - https://junit.org/
- TestContainers - https://testcontainers.com/
All dockerfiles and docker compose files are placed in src/main/docker
.
Dockerfiles use Java 21 image (Eclipse Temurin).
Simplest image - requires built uber jar in target.
To build:
./mvnw package -DskipTests
docker build -f src/main/docker/Dockerfile-outsidejar -t todoapp-outsidejar .
To run:
docker run -p 7070:7070 todoapp-outsidejar:latest -it
Solves "works on my machine" problem - app is built in the container.
Requires both JDK and JRE - to build and run. There is no cache.
To build:
docker build -f src/main/docker/Dockerfile-insidejar -t todoapp-naive .
To run:
docker run -p 7070:7070 todoapp-naive:latest -it
Improved version of #2 - uses multiple stages:
- download and cache dependencies (uses JDK image) in
dependencies
stage. - build and package app (uses JDK image) in
package
stage. - run app (uses JRE image) in
final
stage.
As a result the final image contains only the app (shaded, uber jar).
Uber jar is created using maven-shade-plugin
- classes from all dependencies
are put in the final jar.
To build:
docker build -f src/main/docker/Dockerfile-shaded -t todoapp-multistage1 .
To run:
docker run -p 7070:7070 todoapp-multistage1:latest -it
Improved version of #3 - stages are the same (dependencies
, package
and final
), but
image used is alpine
(instead of Ubuntu one).
Uses maven-jar-plugin
(configuration in pom.xml:92-105
):
- in
dependencies
stage to copy dependencies todependency
directory in repository layout. - in
package
stage to build jar with manifest file containing main class and classpath (fordependency
directory).
In final
stage dependency from dependencies
are copied.
As a result the final image contains only the app (shaded, uber jar).
To build:
docker build -f src/main/docker/Dockerfile-dependency -t todoapp-multistage2 .
To run:
docker run -p 7070:7070 todoapp-multistage2:latest -it
To debug:
docker run -p 7070:7070 -p 5005:5005 -e JVM_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005" todoapp-multistage2:latest -it
- Run remote debug on port 5005.
Running only dependencies:
- MongoDB - exposed at 27017, with data stored in host (
src/main/docker/mongo-data
) - Mongo Express - simple GUI Mongo, exposed at 8081.
Running the Todos app (memory version) only - available on 7070 port.
Running the Todos app (memory version) only - built from the Dockerfile. App is available on port 7070.
Running the Todos app (mongo version) and Mongo + Mongo Express.
Data is stored within Mongo container only.