6

I am trying to figure out how to build a spring boot docker image using fabric8 docker-maven-plugin. The documentation contains bit and bytes and I'm obviously missing something. Does anyone have a full pom.xml example for it?

5 Answers 5

11

The fabric8-maven-plugin documentation is pretty hard to dig through if you just want to get started quickly, so here's a quick example for everything you need to get a Docker image built.

First, make sure docker is on your path and the Docker daemon is running. Run docker ps and ensure a response like this:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Add this to your pom.xml and run this with mvn clean package docker:build

<build>
    <plugins>
        <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>fabric8-maven-plugin</artifactId>
            <configuration>
                <verbose>true</verbose>
                <images>
                    <image>
                        <!-- Replace your-project-name -->
                        <name>your-project-name:${project.version}</name>

                        <build>

                            <!-- This is the same FROM used in a Dockerfile -->
                            <from>vixns/java8</from>

                            <!-- This is the same ENTRYPOINT used in a Dockerfile -->
                            <entryPoint>
                                <exec>
                                    <arg>java</arg>
                                    <!-- This extra argument is so Tomcat can start faster due to lack of entropy -->
                                    <arg>-Djava.security.egd=file:/dev/./urandom</arg>
                                    <arg>-jar</arg>
                                    <!-- /maven/ is the default dir that the plugin copies your artifact to -->
                                    <arg>/maven/${project.artifactId}.${project.packaging}</arg>
                                </exec>
                            </entryPoint>

                            <assembly>
                                <!-- This is a predefined assembly.xml that will only copy your final artifact to the Docker image -->
                                <descriptorRef>artifact</descriptorRef>
                            </assembly>
                        </build>
                    </image>
                </images>
            </configuration>
        </plugin>
    </plugins>
</build>

Note: you'll need additional setup if you want to use the mvn docker:start command. You don't have to use it, you can use the standard docker command if you'd like.

2
  • works great for me, but adding project.version to arg tag. <arg>/maven/${project.artifactId}-${project.version}.${project.packaging}</arg>. Useful when no buid.finalname is set. Thanks dustin! Feb 5, 2019 at 13:24
  • That final artifect is too fat, any ideas to layer the external depencies?
    – Jerry Chin
    Dec 24, 2022 at 11:31
2

Here is one of Working example using fabric8. This project uses spring boot docker image and then link it to mongodb.

    <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.20.0</version>
            <configuration>
                <!--<dockerHost>tcp://REMOTE_IP:2375</dockerHost> -->
                <images>
                    <image>
                        <name>springboot-mongo-dockerimage:${project.version}</name>
                        <alias>springboot-mongo-dockerimage</alias>
                        <build>
                            <dockerFileDir>${project.basedir}</dockerFileDir>

                        </build>
                        <run>
                            <namingStrategy>alias</namingStrategy>
                            <dependsOn>
                                <container>mongo</container>
                            </dependsOn>

                            <links>
                                <link>mongo</link>
                            </links>
                            <ports>
                                <port>8080:8080</port>
                            </ports>
                        </run>
                    </image>
                </images>
            </configuration>

            <executions>
                <execution>
                    <id>start</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                        <goal>build</goal>
                        <goal>start</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

You can follow this link for step by step instructions

However rather than building Image from fabric8 maven plugin, you can use the Dockerfile which I find more convenient using and that is why you would notice

<build>
   <dockerFileDir>${project.basedir}</dockerFileDir>
</build>

DockerFile

    FROM java:8
VOLUME /tmp
ADD target/Boot-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://mongo/test", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

If you want to push your images to Docker hub registry then you can use this link.

0

if you don't have to use that plugin, I recommend spotify's docker-maven-plugin. after you setup, you can do mvn clean package docker:build to build the docker image.

your pom.xml looks like this:

...
<properties>
   <docker.image.prefix>springio</docker.image.prefix>
</properties>
<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>
                <buildArgs>
                    <finalName>${project.build.finalName}.jar</finalName>
                </buildArgs>
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>
    </plugins>
</build>
...

Your Dockerfile in src/main/docker looks something like this:

FROM openjdk:8u102-jre
ARG finalName
ADD $finalName /my-app.jar
ENTRYPOINT ["java","-jar","/my-app.jar"]

References:

https://spring.io/guides/gs/spring-boot-docker

https://github.com/spotify/docker-maven-plugin

4
  • 1
    com.spotify:docker-maven-plugin does not support aws authentification at the moment (30 of may 2018). Yes they have an issue in github, but still it is not supported. So if you want to publish into aws repository spotify is not for you right now.
    – Cherry
    May 30, 2018 at 16:30
  • The question doesn't mention anything about AWS though. You can still use CI/CD to achieve that. We are using this plugin with Jenkins and are able to publish our images to Amazon Elastic Container Registry (ECR).
    – burcakulug
    May 31, 2018 at 16:53
  • 1
    question is about fabric8 docker-maven-plugin not com.spotify.docker-maven-plugin Feb 5, 2019 at 13:08
  • true, but the upvotes mean it helped some people as an alternative answer. better and more relevant answers are meant to pass this one as they get more upvotes.
    – burcakulug
    Feb 6, 2019 at 16:55
0

Here is an example of an inline assembly configuration

Add this lines to your pom file, and use mvn docker:build

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.20.0</version>
    <configuration>
        <verbose>true</verbose>
        <images>
            <image>
                <name>demo/helloworld</name>
                <build>
                    <from>openjdk</from>
                    <tags>
                        <tag>latest</tag>
                        <tag>${project.version}</tag>
                    </tags>
                    <assembly>
                        <descriptorRef>artifact</descriptorRef>
                    </assembly>
                    <cmd>java -jar maven/${project.artifactId}-${project.version}.jar</cmd>
                </build>
            </image>
        </images>
    </configuration>
</plugin>
0
Dockerfile: 
... 
ADD target/<file-name>.jar <dest-dir> 
...

Location of Dockerfile: src/main/docker
Maven io.fabric8 docker plugin configuration

<configuration>
    <images>
        <image>
            <name>${docker.image.prefix}/${docker.image.name}</name>
            <alias>${project.artifactId}</alias>
            <build>
                <contextDir>${project.basedir}</contextDir>
                <tags>
                    <tag>latest</tag>
                </tags>
            </build>
        </image>
    </images>
    <resources>
        <resource>
            <targetPath>${project.basedir}</targetPath>
            <directory>${project.build.directory}</directory>
            <include>${project.build.finalName}.war</include>
        </resource>
    </resources>
</configuration>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.