简体   繁体   中英

Dockerize my spring boot application without using maven to build the image

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM