8
votes

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)

2
Gradle or Maven is irrelevant, as you have written in the last paragraph, a Spring Boot app is just a .jar, how it was generated does not matter. That is why the default Java docker build image is used. - Adam Siemion

2 Answers

7
votes

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.

3
votes

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