Srini
Srini

Reputation: 187

Docker :: Issue with Dockerfile - target folder not available

I have a spring boot project and trying to build the project with a Docker file on circleci continuous integration server.

The problem is that I only have my source code in my github repo; no target directory or jar file. My Dockerfile is not able to get the final artifact (packaging the application to a jar).

Please check the Docker file and suggest a way forward.

FROM java:8
EXPOSE 8090
VOLUME /tmp
ADD target/spring-boot-rest-0.3.0.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=container","-jar","/app.jar"]

Upvotes: 0

Views: 8064

Answers (2)

Robert Moskal
Robert Moskal

Reputation: 22553

You need to somehow package a jar to build your docker image.

You can install maven in your dockerfile and build your project when you build the container. This amounts to automating the steps described in the other answers.

The example below assumes your java source is in the /code directory and your maven package target generating a jar called target/spring-boot-rest-0.3.0.jar:

FROM java:8
EXPOSE 8090
VOLUME /tmp
WORKDIR /code

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

ADD pom.xml /code/pom.xml  
RUN ["mvn", "dependency:resolve"]  
RUN ["mvn", "verify"]

# Adding source, compile and package into a fat jar
ADD src /code/src  
RUN ["mvn", "package"]

ADD target/spring-boot-rest-0.3.0.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=container","-jar","/app.jar"]

However, that doesn't seem to be the way most java folks do it (though it is how you'd containerize a node or a python app).

People seem to build docker images with maven. circl.ci supports maven builds, so that's probably what you should do. This is the plugin I use.There are a bunch:

https://github.com/spotify/docker-maven-plugin/blob/master/README.md

Once you have your pom.xml file configured you'll instruct Circl.ci to create your docker file with a command like this:

mvn clean package docker:build

The process will be the similar whichever plugin you use.

Upvotes: 1

Kyle Anderson
Kyle Anderson

Reputation: 7031

Build Application

First you need to build your application with Maven, Gradle, etc. This will produce a Jar file in your target directory.

Example:

mvn clean package

Build Docker Image

Next you'll build your Docker Image which will use the Jar file produced in the previous step. Make sure that the path you've specified to your Jar file is relative to the location of your Dockerfile (This is important!)

Example:

docker build -t example-company/example-image .

If you continue to have issues please share any error logs or a GitHub repo link.

Upvotes: 1

Related Questions