我有一个依赖于某些基本模块的Java服务模块。
这是我的Dockerfile,它分为三个阶段,一个阶段用于构建基础,一个阶段用于构建服务,另一阶段用于运行时。
FROM maven:3.6.1-jdk-12 AS build-base
RUN mkdir -p /build
WORKDIR /build
COPY src/com.my.app/Base/pom.xml /build
RUN mvn -B dependency:resolve dependency:resolve-plugins
COPY src/com.my.app/Base/src /build/src
RUN mvn clean package
FROM maven:3.6.1-jdk-12 AS build-service
RUN mkdir -p /build
WORKDIR /build
COPY --from=build-base /build/target/Base-1.0-jar-with-dependencies.jar Base.jar
RUN mvn install:install-file \
-Dfile=Base.jar \
-DgroupId=com.my.app \
-DartifactId=Base \
-Dversion=1.0 \
-Dpackaging=jar \
-DgeneratePom=true
COPY src/com.my.app/Service/pom.xml /build
RUN mvn -B dependency:resolve dependency:resolve-plugins
COPY src/com.my.app/Service/src /build/src
RUN mvn clean package
FROM openjdk:12-alpine AS runtime
COPY --from=build-service /build/target/Service-1.0.jar /Service.jar
ENTRYPOINT ["java", "-jar","/Service.jar"]
我正在使用用于this article的多阶段构建的缓存范例在CI服务器上构建它。
docker pull gcr.io/mugen/service:latest-build-base || :
docker build \
--target build-base \
-t gcr.io/mugen/service:latest-build-base \
--cache-from gcr.io/mugen/service:latest-build-base \
.
docker pull gcr.io/mugen/service:latest-build-service || :
docker build \
--target build-service \
-t gcr.io/mugen/service:latest-build-service \
--cache-from gcr.io/mugen/service:latest-build-base \
--cache-from gcr.io/mugen/service:latest-build-service \
.
docker pull gcr.io/mugen/service:latest || :
docker build \
-t gcr.io/mugen/service:latest \
--cache-from gcr.io/mugen/service:latest-build-base \
--cache-from gcr.io/mugen/service:latest-build-service \
--cache-from gcr.io/mugen/service:latest \
.
docker push gcr.io/mugen/service:latest-build-base
docker push gcr.io/mugen/service:latest-build-service
docker push gcr.io/mugen/service:latest
它对于我使用过的其他docker镜像非常有效,但是对于此构建,我总是得到using cache
的所有内容,直到COPY --from=build-base ...
的步骤为止:
Step 10/16 : WORKDIR /build
---> Using cache
---> 4d888270a260
Step 11/16 : COPY --from=build-base /build/target/Base-1.0-jar-with-dependencies.jar Base.jar
---> 6498c821d566
Step 12/16 : RUN mvn install:install-file -Dfile=Base.jar -DgroupId=com.my.app -DartifactId=Parser -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true
---> Running in e9f8d0c39eee
[INFO] Scanning for projects...
此问题在build-server
阶段和runtime
阶段都重复出现,因此在实践中,我在中间阶段运行了两次,这比运行没有每阶段缓存的整个构建还要糟糕
我知道jar并没有改变,因为它的映像是使用100%缓存使用率构建的。