Search code examples
dockerdockerfiledocker-multi-stage-build

Docker Multistage Build Fails on copy from previous stage


I am working on a multistage docker build file in an attempt to add an additional war file to our base tomcat image. I am running this locally on a windows 10 workstation with Docker for Windows version 2.3.0.4 which uses docker engine version 19.03.12

My original dockerfile looked like this

FROM tomcat:9.0.21-jdk8-openjdk

RUN rm -rf /usr/local/tomcat/webapps/*

COPY logging.properties /usr/local/tomcat/conf
COPY tomcat-users.xml /usr/local/tomcat/conf
COPY jt400-jdk8-9.7.jar /usr/local/tomcat/lib
COPY mysql-connector-java-8.0.12.jar /usr/local/tomcat/lib
COPY ojdbc6.jar /usr/local/tomcat/lib
COPY hazelcast-all-3.12.jar /usr/local/tomcat/lib
COPY hazelcast-tomcat85-sessionmanager-1.1.3.jar /usr/local/tomcat/lib
COPY hazelcast-client.xml /usr/local/tomcat/lib
COPY applicationinsights-agent-2.5.1.jar /usr/local/tomcat/lib

What I want to do is build psi-probe from source and add the war file to this base image. Here is what I have thus far

FROM maven:3.6.3-openjdk-8 as buildprobe

RUN git clone https://github.com/psi-probe/psi-probe && cd psi-probe

WORKDIR /psi-probe

RUN mvn package && ls -l /psi-probe && ls -l /psi-probe/psi-probe-web/target

FROM tomcat:9.0.21-jdk8-openjdk

RUN rm -rf /usr/local/tomcat/webapps/*

COPY --from=buildprobe /psi-probe/psi-probe-web/taget/probe.war /usr/local/tomcat/webapps

I can run docker build --target buildprobe -t buildprobe -f Dockerfile . and the output is built.

NOTE: I added the ls commands after mvn package in an attempt to "debug" what going on

The output from maven package and both ls commands is (truncated)

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for psi-probe 3.5.1-SNAPSHOT:
[INFO]
[INFO] psi-probe .......................................... SUCCESS [01:07 min]
[INFO] psi-probe-core ..................................... SUCCESS [01:53 min]
[INFO] psi-probe-rest ..................................... SUCCESS [  5.945 s]
[INFO] psi-probe-tomcat7 .................................. SUCCESS [ 14.019 s]
[INFO] psi-probe-tomcat85 ................................. SUCCESS [ 13.403 s]
[INFO] psi-probe-tomcat9 .................................. SUCCESS [  9.391 s]
[INFO] psi-probe-web ...................................... SUCCESS [ 39.060 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  04:30 min
[INFO] Finished at: 2020-08-12T14:39:19Z
[INFO] ------------------------------------------------------------------------
(ls -l /psi-probe)
...
-rw-r--r-- 1 root root 70248 Aug 12 14:29 pom.xml
drwxr-xr-x 1 root root  4096 Aug 12 14:36 psi-probe-core
drwxr-xr-x 1 root root  4096 Aug 12 14:38 psi-probe-rest
drwxr-xr-x 3 root root  4096 Aug 12 14:29 psi-probe-tomcat10
drwxr-xr-x 1 root root  4096 Aug 12 14:38 psi-probe-tomcat7
drwxr-xr-x 1 root root  4096 Aug 12 14:38 psi-probe-tomcat85
drwxr-xr-x 1 root root  4096 Aug 12 14:38 psi-probe-tomcat9
drwxr-xr-x 1 root root  4096 Aug 12 14:38 psi-probe-web
drwxr-xr-x 3 root root  4096 Aug 12 14:29 src
...
(ls -l /psi-probe/psi-probe-web/target)
drwxr-xr-x 2 root root     4096 Aug 12 14:38 classes
drwxr-xr-x 3 root root     4096 Aug 12 14:39 jspc
drwxr-xr-x 2 root root     4096 Aug 12 14:39 maven-archiver
drwxr-xr-x 7 root root     4096 Aug 12 14:39 probe
-rw-r--r-- 1 root root 27372423 Aug 12 14:39 probe.war
drwxr-xr-x 2 root root     4096 Aug 12 14:39 test-classes
-rw-r--r-- 1 root root    33350 Aug 12 14:39 webfrag.xml

However when the build gets to stage 1 (FROM tomcat:9.0.21-jdk8-openjdk) it fails on the COPY command with

COPY failed: stat /var/lib/docker/overlay2/81826a6b59d58818c342f1d5536489f4658be5e8557084ec572780aaf6ea19bc/merged/psi-probe/psi-probe-web/taget/probe.war: no such file or directory

I have a similar dockerfile for an angular app I am working on. This files reads as so:

FROM node AS build

WORKDIR /app

ENV PATH /app/node_modules/.bin:$PATH

COPY package.json /app/package.json
RUN npm install && npm install -g @angular/cli

COPY . /app

RUN ng build --output-path=dist

FROM nginx

COPY --from=build /app/dist /usr/share/nginx/html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

In this file the COPY from the previous stage works fine and the image builds

I'm simply not sure what is going wrong trying to build psi-probe from source and then including the output in my tomcat image.


Solution

  • This will do the job:

    FROM maven:3.6.3-openjdk-8 as buildprobe
    RUN git clone https://github.com/psi-probe/psi-probe && cd psi-probe
    WORKDIR /psi-probe
    RUN mvn package && ls -l /psi-probe && ls -l /psi-probe/psi-probe-web/target
    
    FROM tomcat:9.0.21-jdk8-openjdk
    RUN rm -rf /usr/local/tomcat/webapps/*
    COPY --from=buildprobe /psi-probe/psi-probe-web/target/probe.war /usr/local/tomcat/webapps
    

    You were missing an r