HiveBrain v1.2.0
Get Started
← Back to all entries
patterndockerMinor

What is the best way to share maven repository using Docker build (dynamic) image with maven?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
theimagewhatdockerwithwaydynamicrepositoryusingshare

Problem

We have created a Docker build container to be run by Jenkins for Java based projects. We are using Maven for building our software. The problem we are facing is that, the builds are taking way to long to finish and the main reason for that is that maven is re-downloading all the dependencies for every single build, maven is run from inside the build container and not from the pipeline step.

We thought about using a shared folder but we have dynamic build agents so no shared folder. Is there anyone who is using the same approach and is there a better approach to this?

Solution

You might use docker builder pattern. Briefly you need to create Dockerfile.build which adds pom.xml and run mvn dependency:resolve:

FROM maven:latest
ADD ./pom.xml /src/pom.xml
WORKDIR /src
RUN mvn dependency:resolve


Rebuild that image every time prior build. Docker will use cached image if ./pom.xml has not changed.

Docker 17.05 introduced 'multi-stage builds' feature which implements this pattern. More information is available here: https://docs.docker.com/engine/userguide/eng-image/multistage-build/

Code Snippets

FROM maven:latest
ADD ./pom.xml /src/pom.xml
WORKDIR /src
RUN mvn dependency:resolve

Context

StackExchange DevOps Q#2034, answer score: 3

Revisions (0)

No revisions yet.