Ricardo Ferreira
Ricardo Ferreira

Reputation: 186

Docker and Java IDE integration

I'll start by saying that I'm not a Java developer and also I'm not a Docker expert.

In order to minimize the gap between frontend and backend (in this specific case, Java) developers I started to put some docker images in place with java and maven and after the build I start a docker container with a volume pointing to the java project (and frontend developers don't have to worry about dependencies or how to run backend services).

Already here I have a question. I've seen other people building an image with the actual code inside instead of attaching it later, so what's the best case (if there's one)? I've done this way since I can reuse that image for "every" project and avoid building different images.

For starting/stopping/restarting docker containers I created a script that does all of that, so I can make some changes to the code, bring it down and up again.

It kinda works, and what I mean is, I'm well aware this is not a normal workflow of a Java developer to do that kind of stuff from a console. So now, to the most important question, how do you integrate docker with a Java IDE? I know that you can create custom build/run commands but I also read that things like logs are not displayed on the IDE's.

Can someone explain me how are you using Docker + Java IDE's?

Note: Maven is also used for compiling java code, like mvn clean install (if this helps)

Upvotes: 3

Views: 645

Answers (1)

Nathaniel Waisbrot
Nathaniel Waisbrot

Reputation: 24473

I do not use Docker with a Java IDE. I use the IDE (Eclipse) to write and test the code, with Maven to manage the build. Then I have a Dockerfile like this:

FROM java:8

RUN apt-get update || apt-get update
RUN apt-get install -y maven

# Maven installs Java 7, which set itself as the default...
RUN update-alternatives --remove java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

CMD java -jar target/main.jar

# Pull down dependencies here to allow Docker to cache more
ADD pom.xml /opt/app/pom.xml
WORKDIR /opt/app
RUN mvn dependency:go-offline -X

# I use the maven-shade-plugin to build a single jar
ADD src /opt/app/src
RUN mvn package

If you build all your images on one machine, then Docker will cache intelligently and you don't need to do anything more. If you want to run across more machines, or you just want to make it explicit, you could do something like this:

base/Dockerfile:

FROM java:8

RUN apt-get update || apt-get update
RUN apt-get install -y maven
RUN update-alternatives --remove java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

CMD java -jar target/main.jar

$ docker build -t yourorg/java-base:8 ./base/

myapp/Dockerfile:

FROM yourorg/java-base:8

ADD pom.xml /opt/app/pom.xml
WORKDIR /opt/app
RUN mvn dependency:go-offline -X

ADD src /opt/app/src
RUN mvn package

You don't get as big an effect from Docker with Java, because JARs are already pretty portable and well-contained. I suppose it makes it easy to run different Java versions side-by-side. I use it because it allows me to run applications in different languages without needing to know what's inside the container. I have some in Java, some in Python, some in JavaScript, some in Erlang, but they all get started as docker run -d <flags> myorg/myimage:someversion.

Upvotes: 2

Related Questions