How do I point a docker image to my .m2 directory for running maven in docker on a mac?
Asked Answered
W

4

21

When you look at the Dockerfile for a maven build it contains the line:

VOLUME /root/.m2

Now this would be great if this is where my .m2 repository was on my mac - but it isn't - it's in

/Users/myname/.m2

Now I could do:

But then the linux implementation in Docker wouldn't know to look there. I want to map the linux location to the mac location, and have that as part of my vagrant init. Kind of like:

ln /root/.m2 /Users/myname/.m2

My question is: How do I point a docker image to my .m2 directory for running maven in docker on a mac?

Wennerholn answered 26/3, 2016 at 10:13 Comment(0)
T
10

How do I point a docker image to my .m2 directory for running maven in docker on a mac?

You rather point a host folder (like /Users/myname/.m2) to a container folder (not an image)

See "Mount a host directory as a data volume":

In addition to creating a volume using the -v flag you can also mount a directory from your Docker daemon’s host into a container.

$ docker run -d -P --name web -v /Users/myname/.m2:/root/.m2 training/webapp python app.py

This command mounts the host directory, /Users/myname/.m2, into the container at /root/.m2.
If the path /root/.m2 already exists inside the container’s image, the /Users/myname/.m2 mount overlays but does not remove the pre-existing content.
Once the mount is removed, the content is accessible again.
This is consistent with the expected behavior of the mount command.

Titillate answered 26/3, 2016 at 10:34 Comment(5)
By some reasons this not working for me. In build stage maven still download dependencies... Any suggestions?Upcast
Yes, I would like to know more: what host OS, image OS, maven version are you using? Could you detail those information in a new question?Titillate
It is just docker cant share volumes while build. Just in run, github.com/docker/docker/issues/14080Upcast
Very confusing why you're mounting a Maven folder and running a Python script :) In any case, wouldn't this cause permissions issues when maven (eventually) downloads and writes files within the volume as its uid/gid?)Motorcade
@Motorcade like any mounted folder, you must make sure your image uses the right uid/gid if it intent to persist data in said mounted folder indeed.Titillate
I
9

To share the .m2 folder in build step you can overwrite the localRepository value in settings.xml.

Here is the Dockerfile snippet I used to share my local .m2 repository in docker.

FROM maven:3.5-jdk-8 as BUILD

RUN echo \
    "<settings xmlns='http://maven.apache.org/SETTINGS/1.0.0\' \
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' \
    xsi:schemaLocation='http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd'> \
        <localRepository>/root/Users/myname/.m2/repository</localRepository> \
        <interactiveMode>true</interactiveMode> \
        <usePluginRegistry>false</usePluginRegistry> \
        <offline>false</offline> \
    </settings>" \
    > /usr/share/maven/conf/settings.xml;

COPY . /usr/src/app
RUN mvn --batch-mode -f /usr/src/app/pom.xml clean package

FROM openjdk:8-jre
EXPOSE 8080 5005
COPY --from=BUILD /usr/src/app/target /opt/target
WORKDIR /opt/target
ENV _JAVA_OPTIONS '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005'
ENV swarm.http.port 8080

CMD ["java", "-jar", "app-swarm.jar"]
Invent answered 25/9, 2018 at 21:5 Comment(1)
You must still be mounting a volume here to make /root/Users/myname/ work within the container? You also don't need a "target" folder for a JAR to be ranMotorcade
H
2

Here are the Dockerfiles and docker-compose for example project containing one spring service and any other services;

Spring-service dockerfile

FROM maven:3.5-jdk-8-alpine 

WORKDIR /app

COPY . src

CMD cd src ; mvn spring-boot:run

docker-compose.yml

version: '3'

services:

  account-service:
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    volumes:
      - "${HOME}/.m2:/root/.m2"

Here in docker-compose we make volumes for our local .m2 repo and container one.

Hallah answered 3/3, 2020 at 13:48 Comment(1)
You should consider not using mvn to run the app. The JDK isn't needed, and this causes larger image sizesMotorcade
S
0

I am using maven:3-amazoncorretto-17 as my base image.

Using some info from Arun A's answer I was able to come up with a pretty simple solution. The tricky part (for me) was figuring out where the settings.xml needed to go inside the container. Also, this example is mounting just settings.xml.

Using docker-compose:

    volumes:
      - "~/.m2/settings.xml:/usr/share/maven/conf/settings.xml"

Or docker run -v

$ docker run -v ~/.m2/settings.xml:/usr/share/maven/conf/settings.xml maven:3-amazoncorretto-17 

This syntax should work on both Windows and Mac.

Skidway answered 21/4, 2023 at 22:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.