Docker multistage ONBUILD not executing first stage onbuild commands

Viewed 629

I'm trying to create a multistage ONBUILD image for a Java project, see the example below. First iteration only seems to see the ONBUILD command in the last stage while inspecting the build result.

FROM maven:3.6.3-jdk-11-slim AS build
RUN mkdir /project
WORKDIR /project
ONBUILD COPY pom.xml /project
ONBUILD RUN mvn dependency:go-offline --batch-mode
ONBUILD COPY . /project
ONBUILD RUN mvn package

FROM adoptopenjdk/openjdk11:jre-11.0.6_10-alpine
RUN mkdir /app
WORKDIR /app
ONBUILD COPY --from=build /project/target/*.jar /app/application.jar

CMD ["java", "-jar", "application.jar"]

I decided to split the stages in 2 images and then combining these in the application image.

FROM maven:3.6.3-jdk-11-slim AS build
RUN mkdir /project
WORKDIR /project
ONBUILD COPY pom.xml /project
ONBUILD RUN mvn dependency:go-offline --batch-mode
ONBUILD COPY . /project
ONBUILD RUN mvn package
FROM adoptopenjdk/openjdk11:jre-11.0.6_10-alpine
RUN mkdir /app
WORKDIR /app
ONBUILD COPY --from=build /project/target/*.jar /app/application.jar

CMD ["java", "-jar", "application.jar"]

Inspecting both images show the correct ONBUILD commands, after combining these 2 images in the following way:

FROM maven-jdk-11-onbuild:latest as build

FROM jre-11-onbuild:latest

The result, it only executes the ONBUILD COPY command from the second image.

[+] Building 0.2s (6/6) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                   0.0s
 => => transferring dockerfile: 120B                                                                                                                                           0.0s
 => [internal] load .dockerignore                                                                                                                                              0.0s
 => => transferring context: 34B                                                                                                                                               0.0s
 => [internal] load metadata for docker.io/library/jre-11:onbuild                                                                                                                                                                                                           0.0s
 => CACHED [build 1/1] FROM docker.io/library/maven-jdk-11:onbuild                                                                                                     0.0s
 => CACHED [stage-1 1/1] FROM docker.io/library/jre-11:onbuild                                                                                                                                                                                                              0.0s
 => ERROR [stage-1 2/1] COPY --from=build /app/target/*.jar /app/application.jar                                                                                               0.1s
------
 > [stage-1 2/1] COPY --from=build /app/target/*.jar /app/application.jar:
------
lstat /var/lib/docker/overlay2/daced0019dbfe2605e586b5607160adb3358c22054ae2762a0338b3681eddd33/merged/app/target: no such file or directory

It looks like the first stage is cached for some reason. Is it possible to combine multistage builds with ONBUILD logic?

0 Answers
Related