简体   繁体   English

不用我的Maven来构建我的Spring Boot应用程序而构建镜像

[英]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, 我想通过docker容器为spring boot应用程序提供服务,而在设置时,它需要maven插件(fabric8)为容器构建docker映像,我不明白,

  • 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/ ) 为什么需要Maven构建映像(甚至Spring Boot官方文档也建议这样做。https://spring.io/guides/gs/spring-boot-docker/
  • Is there any best practice to dockerize my spring boot app? 有什么最佳实践可对我的Spring Boot应用进行docker化? Please help me with this, thanks in advance 请帮助我,在此先感谢

There are two ways of building docker image 有两种构建docker映像的方法

1. Build a Docker Image with Maven 1.用Maven构建一个Docker镜像

Add dockerfile maven plugin 添加dockerfile maven插件

<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 使用命令行构建Docker映像

./mvnw install dockerfile:build

2. Build a Docker image using Docker command 2.使用Docker命令构建Docker映像

  • cd into project directory containing the Dockerfile cd进入包含Dockerfile的项目目录
  • Run maven command 运行Maven命令

    ./mvnw clean install ./mvnw全新安装

  • Run the docker build command 运行docker build命令

    docker build . 码头工人建设。

    docker build -f /path/to/Dockerfile 码头工人建立-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. Docker映像是Spring Boot无法提供的,因为Spring Boot本身是一个运行时框架,它告诉JVM如何运行您的代码。 Instead, Docker is about how to ship and run JVM itself. 取而代之的是,Docker是关于如何交付和运行JVM本身的。 So, Spring Boot is a run-time framework, and Docker image is not something that you do at run-time. 因此,Spring Boot是一个运行时框架,而Docker映像不是您在运行时要做的。 You can build Docker image only at build time, when your artifact (and Spring Boot) is not running yet. 您只能在构建时(您的工件(和Spring Boot)尚未运行)构建Docker映像。

Maven is a build tool, so when Maven performs a build, it can include building Docker image in whole build process. Maven是一个构建工具,因此当Maven执行构建时,它可以包括在整个构建过程中构建Docker映像。 This can be done not only with Maven, but with any build tool that has Docker plugin. 不仅可以使用Maven,而且可以使用任何具有Docker插件的构建工具来完成。 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只是一个流行的构建工具,因此它经常在其他框架手册中使用,但实际上Maven,Spring Boot和Docker之间没有依赖关系。 Maven just has all the tools to build Spring boot applications and also build Docker images. Maven拥有用于构建Spring Boot应用程序以及构建Docker映像的所有工具。

To dockerize your app, you don't need Maven, but you need to somehow include building Docker image to build script for your application. 要对您的应用进行Docker化,您不需要Maven,但是您需要以某种方式包括构建Docker映像来为您的应用程序构建脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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