2

I have a Java project that builds a jar file that is then ran on a Jboss EAP server. The jar file is built using the maven command:

mvn clean install

Currently I am building the jar manually using maven then adding it to my dockerfile as follows:

# rest of dockerfile...

EXPOSE 8080 9990
ADD app.jar "/opt/jboss/jboss-eap-6.2/standalone/deployments/"

RUN ["chmod", "+rx", "/opt/jboss/jboss-eap-6.2/"]
ENTRYPOINT ["sh","/opt/jboss/jboss-eap-6.2/bin/standalone.sh"]

How can I add the maven command into my dockerfile so that a developer does not need to install maven and build it themselves first?

i.e. the steps will just be:

1. checkout the code from git
2. run dockerfile

I think the best approach is to use a multi stage build similar to what is suggested here? However I am not sure how to implement this.

java12399900
  • 1,485
  • 7
  • 26
  • 56

2 Answers2

1

The Dockerfile examples in Mark O'Connor's answer to the question you've linked to should be very close to what you need. Those are multistage builds because they have multiple FROMs in them. The first stage uses a base image that already contains maven, so no need to install it: FROM maven:3.6.0-jdk-11-slim AS build.

Your current Dockerfile content then becomes the second stage, but you will need to replace your ADD app.jar ... with a COPY which picks the file out of the build stage. Something like

COPY --from=build /path/to/mvn/output/app.jar \
        /opt/jboss/jboss-eap-6.2/standalone/deployments/

The --from=build refers to the AS build from the earlier stage. No other files from the build stage will be copied into the later stage(s), so the final image will consist only of layers from the last FROM onwards.

Editing to add: To address the question of how this prevents another user from needing to do any build and install manually: A new user can do this, instead. Assuming that Dockerfile lives in the root of your git project.

git clone <url> your-app
docker build -t your-img your-app # builds all stages
docker run --rm -it your-img      # runs whatever the final stage produced
peten
  • 26
  • 4
  • This has been very useful, but I am quite confused as my java project is multi module, therefore I cannot just copy src? – java12399900 Dec 23 '20 at 11:51
0

You can say to do all that in a Dockerfile! You can provide instructions to install maven inside Dockerfile first and then run whatever command you need from the installed maven.

For instance,

RUN cd /usr/local && wget -O- http://mirror.ox.ac.uk/sites/rsync.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz | tar -xzf -

to install maven 3.6.3 and then later run a command that does what you need whether it be maven clean or whatever.

I just provided the above command because I had it lying around, but if the version you want is available in yum, the other way would just be to use whatever package manager is installed. Like RUN yum --assumeyes install ________.

You should be able to do something similar with Git. I would read about best practices surrounding that.

spinyBabbler
  • 392
  • 5
  • 19