它可能看似简单的任务,但由于某种原因我无法正确完成。我有一个如下的多阶段Docker文件(首先构建Maven工件,然后构建最终的Docker映像):
FROM openjdk:11.0.7-jdk-slim as build
WORKDIR /workspace/app
COPY mvnw .
COPY .mvn .mvn
COPY pom.xml .
COPY src src
RUN ./mvnw install -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
FROM openjdk:11.0.7-jre-slim
VOLUME /tmp
ARG DEPENDENCY=/workspace/app/target/dependency
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
EXPOSE 8080
ENTRYPOINT ["java","-cp","app:app/lib/*","com.example.Application"]
现在docker-compose.yml如下(我尝试了不同的方法,但没有成功):
version: '3.5'
services:
webapi:
build: .
ports:
- 8080:8080
depends_on:
- postgres
env_file:
- local.env
volumes:
- ~/.m2:/root/.m2
postgres:
image: postgres:12.3
restart: always
ports:
- 5432:5432
我也尝试过使用target
,但是由于某种原因,它不起作用。有人可以帮助我将.m2
文件夹安装到docker build
阶段吗? (目标是在安装了maven存储库的docker中构建工件,这样就无需再次下载所有依赖项。)