eixcs
eixcs

Reputation: 1989

Docker has to build spring app and then run it

I'm not able to make Docker build my application and then run it. I have an excercise where I cannot pre-build the application and then run it, Docker has to do the building.

Here's my Dockerfile:
FROM maven:3.5.3-alpine
WORKDIR /usr/src
COPY . .
RUN mvn clean install

COPY ./target/worker-0.0.1-SNAPSHOT.jar worker-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "worker-0.0.1-SNAPSHOT.jar"]

after doing docker build -t worker .

I get an error:

[INFO] Installing /usr/src/target/worker-0.0.1-SNAPSHOT.jar to /root/.m2/repository/com/heiko/worker/0.0.1-SNAPSHOT/worker-0.0.1-SNAPSHOT.jar
[INFO] Installing /usr/src/pom.xml to /root/.m2/repository/com/heiko/worker/0.0.1-SNAPSHOT/worker-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 41.059 s
[INFO] Finished at: 2018-12-27T16:04:10Z
[INFO] ------------------------------------------------------------------------
Removing intermediate container d8d33baa7558
 ---> 3509c06f8736
Step 5/7 : COPY ./target/worker-0.0.1-SNAPSHOT.jar worker-0.0.1-SNAPSHOT.jar
COPY failed: stat /var/lib/docker/tmp/docker-builder701090107/target/worker-0.0.1-SNAPSHOT.jar: no such file or directory

Upvotes: 0

Views: 582

Answers (1)

erik258
erik258

Reputation: 16302

Is worker-0.0.1-SNAPSHOT.jar something that mvn clean install builds? COPY copies files from the docker host to the docker container (like you use in COPY . ., copy the target directory of the docker build to the WORKDIR, to copy your code into the container).

If you want to move around files within the container, you should use

RUN cp ./target/worker-0.0.1-SNAPSHOT.jar worker-0.0.1-SNAPSHOT.jar

This will create 2 copies of the file in the docker container, one at target/worker...jar and one at ./worker...jar. If you want only one copy, you'd have to use mv (move) instead and move the file in the same layer that created it, otherwise the layering system of docker will preserve the old location anyway. I think you might be able to achieve it with something like:

RUN mvn clean install && mv ./target/worker-0.0.1-SNAPSHOT.jar worker-0.0.1-SNAPSHOT.jar

hint for further optimization of image size & security: you can keep the final image even smaller by using a multi-stage build (https://aboullaite.me/multi-stage-docker-java/ seems to be a good maven explanation.)

Upvotes: 1

Related Questions