What Docker base image (FROM) for Java Spring Boot application?
I am just starting with docker, and I see that FROM inside Dockerfile can define image for Java like
FROM java:8
If I am building using Gradle (or Maven) is the better base image to start to avoid configuring later what is common for Gradle/Maven project?
And of course Spring Boot application is just .jar file inside build output folder, there should be less questions about how to run with Docker (for Java project built with standard build tools)
There's a nice documentation on how to integrate Spring-Boot with Docker: https://spring.io/guides/gs/spring-boot-docker/
Basically you define your dockerfile in src/main/docker/Dockerfile and configure the docker-maven-plugin like this:
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.11</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
Dockerfile:
FROM frolvlad/alpine-oraclejre8:slim
VOLUME /tmp
ADD gs-spring-boot-docker-0.1.0.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
Note that in this example FROM frolvlad/alpine-oraclejre8:slim is a small-footprinted image which is based on Alpine Linux.
You should also be able to use the standard Java 8 image (which is based on Debian and might have an increased footprint) as well. An extensive list of available Java Baseimages can be found here: https://github.com/docker-library/docs/tree/master/openjdk.
I am using the fabric plugin which uses the base docker image fabric8/java-alpine-openjdk8-jdk:1.2. There is no need for a Dockerfile, it is created by the plugin.
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>3.2.28</version>
</plugin>
</plugins>
</build>
Targets are fabric8:build to create docker image and fabric8:push to push docker image registry.
mvn clean install fabric8:build fabric8:push
Related
I'm trying to build a simple docker image, inside a maven project, adding the image build as part of the maven build process:
<build>
<finalName>my-api</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- Docker -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.6</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<!-- <goal>push</goal> -->
</goals>
</execution>
</executions>
<configuration>
<repository>reponame/${project.name}</repository>
<tag>${project.version}</tag>
<skipDockerInfo>true</skipDockerInfo>
</configuration>
</plugin>
</plugins>
</build>
FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 8080
ADD target/*.jar app.jar
ENTRYPOINT [ "sh", "-c", "java -jar /app.jar" ]
But it fails, always get the same error trace, no matter which image I use, the error persists.
Error:
Caused by:
com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException:
java.lang.UnsatisfiedLinkError: could not load FFI provider
jnr.ffi.provider.jffi.Provider
Caused by: java.lang.UnsatisfiedLinkError:
java.lang.UnsatisfiedLinkError:
/private/var/folders/hz/rgppp8250rsdp86kf_tfjvqw0000gp/T/jffi8502916075702391528.dylib:
dlopen(/private/var/folders/hz/rgppp8250rsdp86kf_tfjvqw0000gp/T/jffi8502916075702391528.dylib,
0x0001): tried:
'/private/var/folders/hz/rgppp8250rsdp86kf_tfjvqw0000gp/T/jffi8502916075702391528.dylib'
(fat file, but missing compatible architecture (have 'i386,x86_64',
need 'arm64e')), '/usr/lib/jffi8502916075702391528.dylib' (no such
file)
Other images I tried:
openjdk:13-alpine3.9
openjdk:8-jre-alpine3.9
azul/zulu-openjdk-alpine:17.0.2-17.32.13-arm64
My java version: openjdk version "11.0.13" 2021-10-19 LTS
My Docker version: Docker version 20.10.11, build dea9396
Thanks in advance.
It looks like the dockerfile-maven-plugin uses a runtime based on x86 architecture and won't run on Apple M1 (Arm).
The plugin is now inactive so you should try something else, for example the fabric8-maven-plugin
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.38.1</version>
<executions>
<execution>
<id>build</id>
<phase>pre-integration-test</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
Both of the spotify docker maven plugins are no longer maintained. They need to upgrade their dependency to a version that supports aarch64.
In our case there was significant refactoring needed to move to fabric8's plugin or to use maven exec so we wanted to continue to use spotify plugin.
Fortunately, you can force the plugin to use a particular dependency by adding a <dependencies> section to your plugin section.
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.6</version>
<dependencies>
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-unixsocket</artifactId>
<version>0.38.14</version>
</dependency>
</dependencies>
</plugin>
I met the same problem.According to the error message, it should be a connection error with the docker daemon.It seems that the plugin won't support Arm socket and you can do this:
(1) install socat
brew install socat
(2) set port forwarding
socat TCP-LISTEN:2375,range=127.0.0.1/32,reuseaddr,fork UNIX-CLIENT:/var/run/docker.sock
(3) set environment variable
export DOCKER_HOST=tcp://127.0.0.1:2375
My environment java version is jdk8,Now I want use Maven command mvn package to compile some project.How to specify java version(like java11) to compile it whitout change environment path,because java version is a dynamic arguments in my process
Compiling Sources Using A Different JDK
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable><!-- path-to-javac --></executable>
<compilerVersion>11</compilerVersion>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Well the side effect is we have to edit the pom.xml file.
We may keep different version of pom.xml file, like pom-java11.xml, pom-java17.xml
mvn -f <other pom file> for your task, like mvn -f pom-java11.xml ...
In pom.xml you can define the version of JDK
Like
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.1</spring-cloud.version>
</properties>
I have Java maven project with TestNG
Please see this pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk1.8.0_181\bin\javac.exe</executable>
</configuration>
</plugin>
So inside Windows when I want to run this project from command line I just navigate into this pom.xml folder and then:
mvn clean test
And this will start all my tests.
Now inside this pom.xml i have my javac.exe path so in order to run this project in MAC what I need to add/ change? (I want it to support both OS)
The best practice would probably be to rely on the standard JAVA_HOME environment variable:
<executable>${env.JAVA_HOME}/bin/java</executable>
I have multiple java packages in my java project. Is it possible to run a single package on eclipse using maven. I want to do it with the project level pom. I dont want to create POMs for every package.
Based on your comment, not sure if this is what you are looking for but I use a configuration variable to run test cases for a particular package. You have to add a plugin to the POM to achieve this.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
<configuration>
<includes>
<include>**/${testGroup}/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Now if you want to run the test cases for lets say package org/myPackage you would use a command like this.
mvn test -DtestGroup=org/myPackage
I'm trying to release a library using Maven and perform a site-deploy to sourceforge (I have create an interactive shell first). The release is done by a Jenkins job (using the Maven Release Plugin for Jenkins).
I tried:
-X -e -Dresume=false -Dusername=puce release:prepare release:perform -Darguments="-Dusername=puce"
and
-X -e -Dresume=false -Dusername=puce -Darguments=-Dusername=puce release:prepare release:perform
but both times the job hangs at site:deploy of the first module:
[INFO] --- maven-site-plugin:3.2:deploy (default-deploy) # myproject-parent ---
[INFO] Parent project loaded from repository: myGroupId:myOtherproject-parent:pom:1.0
[INFO] Parent project loaded from repository: myGroupId:myOtherproject-parent:pom:1.0
Using private key: /opt/jenkins/.ssh/id_dsa
When I stop the job, the following gets printed at end:
Password for ${username}#shell.sourceforge.net: channel stopped
which probably means that ${username} wasn't resolved.
How can I resolve the ${username}?
Edit:
Note that the following runs fine:
site-deploy -Psonatype-oss-release -Dusername=puce
Edit 2:
As part of release:perform maven executes the following command:
/usr/share/maven/bin/mvn -s /tmp/release-settings7797889802430474959.xml deploy site-deploy --no-plugin-updates --batch-mode -Psonatype-oss-release -P nexus -f pom.xml
-Dusername=puce doesn't seem to get passed to this maven command...
Also note that help:effective-pom shows the following maven-release-plugin configuration:
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<mavenExecutorId>forked-path</mavenExecutorId>
<useReleaseProfile>false</useReleaseProfile>
<arguments>-Psonatype-oss-release</arguments>
</configuration>
</plugin>
So 'arguments' gets defined and its value seems to reach the embedded maven command instead of the value passed on the command line...
What I've successfully done in the past is as follows:
Define a property in the POM file, e.g.:
<properties>
<release.arguments></release.arguments>
</properties>
Add the POM property to the plugin configuration in the POM file, e.g.;
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<arguments>${release.arguments}</arguments>
...
Pass the argument through the property on the command-line, e.g.:
mvn release:prepare -Drelease.arguments="-N -Prelease"
Hope this helps.
Overriding
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<mavenExecutorId>forked-path</mavenExecutorId>
<useReleaseProfile>false</useReleaseProfile>
<arguments>-Psonatype-oss-release</arguments>
</configuration>
</plugin>
with
<plugin>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<mavenExecutorId>forked-path</mavenExecutorId>
<useReleaseProfile>false</useReleaseProfile>
<arguments>-Psonatype-oss-release -Dusername=${username}</arguments>
</configuration>
</plugin>
in one of the parents did the trick.
It seems to be a bug that the value on the command line doesn't override the value in the POM.
My solution was similar to Sander Verhagen's. I added only line only though.
How I run:
mvn --batch-mode release:prepare -Denvironment=production
My config:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<tag>${artifactId}-${version}-${environment}</tag>
<arguments>-Denvironment=${environment}</arguments>
<releaseProfiles>release</releaseProfiles>
</configuration>
</plugin>
</plugins>
</build>
The difference is that now my submodules use the variable environment and I don't need to define it twice (I.e. -Darguments=-Denvironment=production -Denvironment=production). I also gives me the flexibility of not adding the properties tag.