1

I want to serve my spring boot application via docker container, while setting up, it requires maven plugin(fabric8) to build a docker image for container, i don’t understand,

  • Why maven is required to build an image (even spring boot official documentation suggests to do this . https://spring.io/guides/gs/spring-boot-docker/)
  • Is there any best practice to dockerize my spring boot app? Please help me with this, thanks in advance
3
  • 1
    All you need is a jar, copy the jar to the docker image and run it as java process. Why maven ? how do you package spring boot application as a jar? then you will use spring boot maven/gradle plugin to package app as a jar. Commented Dec 25, 2017 at 4:29
  • @Barath, to build the docker image it requires to be built with maven/gradle plugin !! Commented Dec 25, 2017 at 4:42
  • 1
    Thats right. It is one way of building docker image . other one is to use docker build command to build the docker image. Two ways of building docker image, either use docker command or use plugin. Commented Dec 25, 2017 at 5:28

2 Answers 2

5

There are two ways of building docker image

1. Build a Docker Image with Maven

Add dockerfile maven plugin

<plugins>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.3.6</version>
            <configuration>
                <repository>${docker.image.prefix}/${project.artifactId}</repository>
    <buildArgs>
        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
    </buildArgs>
            </configuration>
        </plugin>
    </plugins>

build the docker image using command line

./mvnw install dockerfile:build

2. Build a Docker image using Docker command

  • cd into project directory containing the Dockerfile
  • Run maven command

    ./mvnw clean install

  • Run the docker build command

    docker build .

    docker build -f /path/to/Dockerfile

Sign up to request clarification or add additional context in comments.

Comments

1

Docker image is something that Spring Boot can't provide, because Spring Boot itself is a runtime framework, it tells to JVM how to run your code. Instead, Docker is about how to ship and run JVM itself. So, Spring Boot is a run-time framework, and Docker image is not something that you do at run-time. You can build Docker image only at build time, when your artifact (and Spring Boot) is not running yet.

Maven is a build tool, so when Maven performs a build, it can include building Docker image in whole build process. This can be done not only with Maven, but with any build tool that has Docker plugin. Maven is just a popular build tool, so it is often used in other framework manuals, but actually there is no dependency between Maven, Spring Boot and Docker. Maven just has all the tools to build Spring boot applications and also build Docker images.

To dockerize your app, you don't need Maven, but you need to somehow include building Docker image to build script for your application.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.