Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 803
hawkeye Avatar asked Mar 26 '16 10:03

hawkeye


People also ask

How do I run a Maven project in Docker?

You can run a Maven project by using the Maven Docker image directly, passing a Maven command to docker run: This is a base image that you can extend, so it has the bare minimum packages needed. If you add custom package (s) to the Dockerfile, then you can build your local Docker image like this:

How do I run a docker image from MVN?

Run the standard command mvn package and on completion you should see a message indicating the Docker image has been successfully created. Once built, we can run our new Docker image. docker run -p 9092:8080 -t demo-application:0.0.1-SNAPSHOT (note change of port to 9092).

How do I build a docker image using jib-Maven?

To build the Docker image, run the command mvn compile com.google.cloud.tools:jib-maven-plugin:2.3.0:dockerBuild On completion you should see a message indicating the Docker image has been successfully created. Once built, we can run our new Docker image. docker run -p 9091:8080 -t demo-application:0.0.1-SNAPSHOT (note change of port to 9091).

How do I run a container in Docker?

Docker Container: A Container is the running instance of an image. The docker run command is used to run a container from an image. Here we are using the ruby image to create the container, but you can use your own image. If the ruby image does not exist on the host machine, Docker will pull that from Docker Hub.


2 Answers

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.

like image 114
VonC Avatar answered Oct 12 '22 12:10

VonC


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"]
like image 45
Arun A Avatar answered Oct 12 '22 11:10

Arun A