gpt4 book ai didi

docker - 多阶段构建无法找到文件

转载 作者:行者123 更新时间:2023-12-02 21:33:43 25 4
gpt4 key购买 nike

这是我第一次使用多阶段程序,运行docker-compose up时出现此错误:

worker_1 | Error: Unable to access jarfile worker-jar-with-dependencies.jar


因此,似乎没有找到该文件。
Docker文件
FROM maven:3.5-jdk-8-alpine AS builder
ADD pom.xml .
RUN mvn dependency:resolve
#RUN /bin/sh -c cd src/
RUN mvn package

FROM openjdk:8-jre-alpine
COPY --from=builder target/worker-jar-with-dependencies.jar .
#RUN java -jar worker-jar-with-dependencies.jar
CMD ["java", "-jar", "worker-jar-with-dependencies.jar"]
以及我的docker-compose.yml的外观
version: '3.0'
services:
poll:
build: poll/.
ports:
- "5000:80"
networks:
- poll-tier
redis:
image: "redis"
expose:
- "6379"
networks:
- back-tier
- poll-tier
worker:
build: worker/.
networks:
- back-tier
db:
image: "postgres"
volumes:
- "db-data:/var/lib/postgresql/data"
networks:
- back-tier
- result-tier
environment:
POSTGRES_PASSWORD: popeye

result:
build: result/.
ports:
- "5001:80"
networks:
- result-tier

volumes:
db-data:
networks:
poll-tier: {}
result-tier: {}
back-tier: {}

最佳答案

看起来您没有在Dockerfile中添加源代码。您的pom仅具有ADD命令,而源中没有一个命令。我想说的是,在“RUN mvn软件包”之前添加“COPY src / src”之类的内容。
不确定是否正在使用springboot,但这是springboot应用程序的多阶段构建示例:

FROM maven:3.6.3-jdk-11 AS builder
WORKDIR /workdir/server
COPY pom.xml /workdir/server/pom.xml
RUN mvn dependency:go-offline

COPY src /workdir/server/src
RUN mvn install
RUN mkdir -p target/depency
WORKDIR /workdir/server/target/dependency
RUN jar -xf ../*.jar

FROM openjdk:11-jre-slim

EXPOSE 8080
VOLUME /tmp
ARG DEPENDENCY=/workdir/server/target/dependency
COPY --from=builder ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=builder ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.company.project.Application"]
您可以在此docker repo中找到以下示例以及其他不错的docker-compose示例: https://github.com/docker/awesome-compose

关于docker - 多阶段构建无法找到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63935745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com