0

I am using the sh file which contains all the maven configuration, secret keys, maven command and all, I I want to execute this sh file from inside the Dockerfile so that when I run the container sh file will execute.

this is how my Docker file looks like:

#using alpine jdk
FROM somewhere.docker.hub/build/alpine:latest AS fetch

#MVN as build tool
FROM somewhere.docker.hub/build/maven:3.5.3-jdk-8 AS build

#Settings.xml for downloading dependencies from nexus repository
ARG MVN_SETTINGS=settings.xml

#Defining the current working repository
WORKDIR /usr/src/app


#Coping the pom.xml into working directory
COPY pom.xml /usr/src/app

COPY ${MVN_SETTINGS} /usr/src/app/settings.xml

# Download the package and make it cached in docker image
#RUN mvn -B -f ./pom.xml -s settings.xml dependency:resolve


#Coping the source code into the working repository
COPY  . /usr/src/app


#Run the sh file
#RUN chmod a+x /usr/src/app/DockerTest.sh

# Not using the full path of DockerTest.sh file because it is already in the work directory as defined above
RUN chmod a+x DockerTest.sh

DockerTest.sh file looks like this.

#!/bin/sh
mvn clean install
mvn test

when I create the docker image and run the image, it shows this error. I have tried lots of things it is still not working, it shows this error

No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]

Arpit Asati
  • 130
  • 2
  • 16

1 Answers1

1

First of all you cannot combine two Docker images by using multiple FROM lines one after another. The reason it's even possible to specify more than one is to enable multi-stage builds.

Second, if you're not using this line:

RUN mvn -B -f ./pom.xml -s settings.xml dependency:resolve

you may as well comment this one too:

COPY pom.xml /usr/src/app

since your POM will be copied along with the rest of the source code.

Third, judging from your Dockerfile, you have several settings.xml files in your working directory and you want to choose one base on a build argument. However, if you have say settings.xml and settings.prod.xml and you set MVN_SETTINGS to settings.prod.xml, then /usr/src/app/settings.xml will be overwritten by this line:

COPY  . /usr/src/app

with settings.xml.

Fourth, mvn clean install runs tests as well, there is no need to do mvn test separately.

Finally, for the issue you have noticed. I'm not sure what you expect to happen when you run the image, but you did not specify any entrypoint or command to be run in your Dockerfile, so your image inherits the default one from its base. Now assuming that by somewhere.docker.hub/build/maven:3.5.3-jdk-8 you actually mean docker.io/library/maven:3.5.3-jdk-8 then the command is mvn and the entrypoint is /usr/local/bin/mvn-entrypoint.sh (see Dockerfile for a slightly newer image here).

With all that said, assuming you want to run DockerTest.sh when your image is run, your Dockerfile should look like this:

FROM maven:3.5.3-jdk-8
ARG MVN_SETTINGS=settings.xml
WORKDIR /usr/src/app
COPY . .
COPY ${MVN_SETTINGS} settings.xml
RUN chmod a+x DockerTest.sh
CMD ./DockerTest.sh

Also, if you actually want to use your settings.xml during build you need to change your DockerTest.sh to:

#!/bin/sh
mvn --settings /usr/src/app/settings.xml clean install

You could also remove unnecessary copying the same file twice by doing:

FROM maven:3.5.3-jdk-8
ARG MVN_SETTINGS=/usr/src/app/settings.xml
ENV MVN_SETTINGS=${MVN_SETTINGS}
WORKDIR /usr/src/app
COPY . .
RUN chmod a+x DockerTest.sh
CMD ./DockerTest.sh

and

#!/bin/sh
mvn --settings "$MVN_SETTINGS" clean install
Konrad Botor
  • 4,765
  • 1
  • 16
  • 26
  • `RUN mvn -B -f ./pom.xml -s settings.xml dependency:resolve` when I uncomment this line to use the dependency from cache, and run the docker image it downloads the dependency, again and again, every time I run the docker file, I want it to get the dependency from docker and do not download when I run the image again. – Arpit Asati Sep 24 '20 at 08:50
  • That's because copying new pom.xml invalidates all the layers below. – Konrad Botor Sep 24 '20 at 09:36
  • Now I am copying all the specific folder also not overriding pom file, still it is downloading dependency again and again- please help. – Arpit Asati Sep 29 '20 at 05:32
  • please see the updated question here https://stackoverflow.com/questions/64115576/unable-to-cache-maven-dependency-in-docker-image – Arpit Asati Sep 29 '20 at 11:30