Search code examples
dockerconfigurationmaven-3

Docker, maven and settings.xml


Given following simple Dockerfile

FROM maven:3.6.3-ibmjava-8-alpine 

# Copy maven settings
COPY settings.xml /usr/share/maven/ref/
COPY pom.xml .

# WHY IS settings.xml HERE NOT CONSIDERED?
ENTRYPOINT ["mvn -X"]

# WHY IS settings.xml HERE NOT CONSIDERED?
#OR: RUN mvn clean install deploy

I copy here the settings.xml as suggested in the maven's docker doc into the image. Now, when I run the container, this will fail with: Could not find artifact com.my.artefactory:my-parent:pom:2.0.2 in central (https://repo.maven.apache.org/maven2) As we can see here, maven tries to resolve things from the default maven repo - despite the fact that I defined my own maven repo in the settings file.

When I remove ENTRYPOINT ["mvn -X"] and connect to the container docker run --rm -it mvn-builder sh, and then manually run mvn -X in the shell, it actually works! There is also the expected output

[DEBUG] Using mirror my-mirror (https://com.my.artefactory/repository/whatever-repo/) for central (http://central).

What do I miss here?


Solution

  • I think the problem is actually the fact that the image I derive from maven:3.6.3-ibmjava-8-alpine already defines an ENTRYPOINT.

    When I run docker run -it --rm my-image-mvn mvn clean install deploy it just works fine - good enough for me...

    EDIT But I still wonder, why does that not work with ENTRYPOINT (or RUN)?

    EDIT 2 Ok, I finally get it, hope it will help somebody else. The maven's image uses an ENTRYPOINT with a shell script, which will make sure the settings.xml is placed into the correct directory. This happens only when you start a container instance.
    In order to actually make it work for e.g. a RUN command, you have to do some of the work yourself.

    Here is an example:

    FROM maven:3.6.3-ibmjava-8-alpine AS maven-builder
    
    # Since we want to execute the mvn command with RUN (and not when the container gets started),
    # we have to do here some manual setup which would be made by the maven's entrypoint script
    RUN mkdir -p /root/.m2 \
        && mkdir /root/.m2/repository
    # Copy maven settings, containing repository configurations
    COPY settings.xml /root/.m2
    
    ...
    
    RUN mvn clean install