Put TomEE Embedded in a Docker container with Maven 2/2
In previous post, we saw how to create a TomEE Embedded Microprofile application easily, just using Maven dependencies. In this post we will just use Jib - a bit like in a jlink series last post - to create a docker from this setup. Comparatively to the jlink post, here we will directly use Jib Maven plugin.
First important point here is that we want to use the javaee-api from tomee-embedded and not another one (in terms of maven it has the classifier tomcat). If you played with scopes to have javaee-api in scope provided, ensure to either make it compile or create a docker module where you can just set the two previous dependencies with your application modules. This should be enough to ensure we have the right scope to all dependencies.
Then we need to add the jib-maven-plugin:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.2</version>
<configuration>
<!-- ... -->
</configuration>
</plugin>
All the question is what will replace the "...". Here we need to do a few choices:
- From which image should we start? Here we will want to use a Java 8 base image since TomEE is not yet officially fully Java11 compatible (last LTS). Between the Java 8 images we will use OpenJDK one to ensure we base our work on something maintained by a trustable community and we will prefer an Alpine based image which will be smaller than others. Finally we will want to use a Java 8u191 image which is docker friendly since the Java 10/11 related option got backported. In short we use this base image: openjdk:8u191-alpine3.9.
- Which image do we want to create? Here it is really up to you, personally I will use rmannibucau/tomee-embedded-microprofile-demo:${project.version}.
- Where our application files will be put and what would be our working directory? You can set two values but personally I like to have both set to the same folder, for this demo I will use /opt/rmannibucau/demo.
- What will be our main class? You can write your own but TomEE Embedded provides one which fits very well this scenario: org.apache.tomee.embedded.FatApp. By default it will deploy the classpath and binds the port 8080. Alternatively you can write a custom main using TomEE Embedded Container API.
- Which ports will be used? If you picked FatApp main then the port 8080 will be bound by default, otherwise ensure to align these image ports with what you coded in your main.
Finally here is the full configuration of jib plugin respecting these previous choices:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.2</version>
<configuration>
<from>
<image>openjdk:8u191-alpine3.9</image>
</from>
<to>
<image>rmannibucau/tomee-embedded-microprofile-demo:${project.version}</image>
</to>
<container>
<mainClass>org.apache.tomee.embedded.FatApp</mainClass>
<ports>
<port>8080</port>
</ports>
</container>
</configuration>
</plugin>
To build the image run the following command:
mvn compile jib:dockerBuild
Tip: Jib does not allow to create an image without application resources, if you created a docker module to create the image instead of reusing a business module then just put an empty java class or resource file.
The previous command should output something like that:
[INFO] Built image to Docker daemon as rmannibucau/tomee-embedded-microprofile-demo:1.0-SNAPSHOT
[INFO] Executing tasks:
[INFO] [==============================] 100,0% complete
If the build suceed you can now run:
docker run -p 8080:8080 rmannibucau/tomee-embedded-microprofile-demo:1.0-SNAPSHOT
Once the application is started you can hit the Microprofile URLs like:
From the same author:
In the same category: