首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在没有JAR的情况下为Spring应用程序构建Docker映像

如何在没有JAR的情况下为Spring应用程序构建Docker映像
EN

Stack Overflow用户
提问于 2018-09-04 09:22:22
回答 2查看 3.2K关注 0票数 2

我遵循这些教程为我的Spring应用程序构建Docker映像,它使用Maven作为构建工具。我在Windows10机器上使用boot2docker VM,将我的项目从Bitbucker存储库克隆到VM上。

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

https://www.callicoder.com/spring-boot-docker-example/

我明白说明,但是,我没有建立一个正确的码头形象。这是我试过的东西。

  1. 为Dockerfile使用Spotify maven插件。尝试运行./mvnw来构建JAR和Docker映像。但是,我没有在boot2docker中安装Java。因此无法运行Maven包装器./mvnw。
  2. 我试图通过Dockerfile构建JAR,该文件基于openjdk:8-JDK-ALPILE映像。我在Dockerfile中添加了RUN ./mvnw package指令。然后运行docker build -t <my_project> .来构建Docker映像。它在运行指令处失败,声称是/bin/sh: mvnw: not found The command '/bin/sh -c mvnw package' returned a non-zero code: 127

我的Dockerfile,位于mvnw所在的目录中:

代码语言:javascript
复制
MAINTAINER myname

VOLUME /tmp

RUN ./mvnw package

ARG JAR_FILE=target/myproject-0.0.1-SNAPSHOT.jar

COPY ${JAR_FILE} app.jar

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

对于1,我需要在Docker引擎所在的操作系统中安装Java。但我认为这不是一个好的实践,因为这降低了便携性。

首先,我不知道如何在Dockerfile中成功地运行./mvnw。其次,我不确定通过Dockerfile构建Spring是否是一个好的实践,因为我没有看到任何“”教程告诉我这样做。

那么,解决我的问题的最佳做法是什么呢?我是码头新来的。非常感谢您的评论和回答!

EN

回答 2

Stack Overflow用户

发布于 2018-09-04 13:04:26

您可以在构建中直接安装maven并运行编译。通常,这将是一个多阶段的构建,以避免将整个jdk包含在您的推送映像中:

代码语言:javascript
复制
FROM openjdk:8-jdk-alpine as build
RUN apk add --no-cache maven
WORKDIR /java
COPY . /java
RUN mvn package -Dmaven.test.skip=true
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/java/target/myproject-0.0.1-SNAPSHOT.jar"]

上面是从我过去做过的相同示例中删除的返工版本。您可能需要调整入口点中的文件名,但关键步骤是安装maven并在构建中运行它。

票数 2
EN

Stack Overflow用户

发布于 2021-05-21 13:31:55

从您的第二个例子中,我认为您误解了Docker如何构建图像。当Docker执行RUN ./mvnw package时,文件mvnw必须存在于正在构建的映像的文件系统中,这意味着在前面的步骤中应该有一个类似于COPY mvnw .的指令--它将把文件从本地文件系统复制到映像中。

在调用./mvnw之前,您可能需要在映像中复制整个项目结构,如@BMitch的响应所示。

此外,正如@BMitch所说,要生成一个小型映像,通常建议使用多阶段构建,其中第一阶段安装每个依赖项,但最终映像只有您的JAR。

您可以尝试如下所示:

代码语言:javascript
复制
# First stage: build fat JAR

# Select base image.
# (The "AS builder" gives a name to the stage that we will need later)
# (I think it's better to use a slim image with Maven already installed instead
#  than ./mvnw. Otherwise you could need to give execution rights to your file
#  with instructions like "RUN chmod +x mvnw".)
FROM maven:3.6.3-openjdk-8-slim AS builder

# Set your preferred working directory
# (This tells the image what the "current" directory is for the rest of the build)
WORKDIR /opt/app

# Copy everything from you current local directory into the working directory of the image
COPY . .

# Compile, test and package
# (-e gives more information in case of errors)
# (I prefer to also run unit tests at this point. This may not be possible if your tests
#  depend on other technologies that you don't whish to install at this point.)
RUN mvn -e clean verify

###

# Second stage: final image containing only WAR files

# The base image for the final result can be as small as Alpine with a JRE
FROM openjdk:8-jre-alpine

# Once again, the current directory as seen by your image
WORKDIR /opt/app

# Get artifacts from the previous stage and copy them to the new image.
# (If you are confident the only JAR in "target/" is your package, you could NOT
#  use the full name of the JAR and instead something like "*.jar", to avoid updating
#  the Dockerfile when the version of your project changes.)
COPY --from=builder /opt/app/target/*.jar ./

# Expose whichever port you use in the Spring application
EXPOSE 8080

# Define the application to run when the Docker container is created.
# Either ENTRYPOINT or CMD.
# (Optionally, you could define a file "entrypoint.sh" that can have a more complex
#  startup logic.)
# (Setting "java.security.egd" when running Spring applications is good for security
#  reasons.)
ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /opt/app/*.war
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52162988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档