我遵循这些教程为我的Spring应用程序构建Docker映像,它使用Maven作为构建工具。我在Windows10机器上使用boot2docker VM,将我的项目从Bitbucker存储库克隆到VM上。
https://spring.io/guides/gs/spring-boot-docker/
https://www.callicoder.com/spring-boot-docker-example/
我明白说明,但是,我没有建立一个正确的码头形象。这是我试过的东西。
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所在的目录中:
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是否是一个好的实践,因为我没有看到任何“”教程告诉我这样做。
那么,解决我的问题的最佳做法是什么呢?我是码头新来的。非常感谢您的评论和回答!
发布于 2018-09-04 13:04:26
您可以在构建中直接安装maven并运行编译。通常,这将是一个多阶段的构建,以避免将整个jdk包含在您的推送映像中:
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并在构建中运行它。
发布于 2021-05-21 13:31:55
从您的第二个例子中,我认为您误解了Docker如何构建图像。当Docker执行RUN ./mvnw package时,文件mvnw必须存在于正在构建的映像的文件系统中,这意味着在前面的步骤中应该有一个类似于COPY mvnw .的指令--它将把文件从本地文件系统复制到映像中。
在调用./mvnw之前,您可能需要在映像中复制整个项目结构,如@BMitch的响应所示。
此外,正如@BMitch所说,要生成一个小型映像,通常建议使用多阶段构建,其中第一阶段安装每个依赖项,但最终映像只有您的JAR。
您可以尝试如下所示:
# 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/*.warhttps://stackoverflow.com/questions/52162988
复制相似问题