3

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?

Bas Koopmans
  • 341
  • 4
  • 14
  • Look for official docs! I see this in ONBUILD command description: "Be careful when putting ADD or COPY in ONBUILD. The “onbuild” image fails catastrophically if the new build’s context is missing the resource being added. Adding a separate tag, as recommended above, helps mitigate this by allowing the Dockerfile author to make a choice." https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ – adampweb Apr 05 '21 at 11:48
  • I'd avoid using `ONBUILD` at all, since it can lead to mysterious behavior when the final image is consumed. As with everything else in a multistage build, though, I'd expect only the contents and metadata of the final stage to matter, and everything else will get ignored unless it gets `COPY`ed into the final image. – David Maze Apr 05 '21 at 13:23

0 Answers0