StillLearningToCode
StillLearningToCode

Reputation: 2461

Building Docker image from Spring Boot Jar

I am trying to follow a tutorial on getting up and running with Docker using a spring boot fat jar. I have everything installed and can run a Hello World example from Docker Hub.

I have created the fat jar using Maven called predictive-text-tree-0.0.1-SNAPSHOT.jar and am using the following as my dokerfile.docker:

FROM java:8
MAINTAINER ltocode
EXPOSE 8080
ADD predictive-text-tree-0.0.1-SNAPSHOT.jar /data/predictive-text-tree-0.0.1-SNAPSHOT.jar
CMD java -jar /data/textpredict.jar

I have the jar and the docker file on the server, and when I run the docker build command I get the following:

~/build# docker build -t predictive-text-tree-0.0.1-SNAPSHOT.jar

invalid argument "predictive-text-tree-0.0.1-SNAPSHOT.jar" for t: Error parsing reference: "predictive-text-tree-0.0.1-SNAPSHOT.jar" is not a valid repository/tag See 'docker build --help'.

How do I build the docker image from a fat jar?

Upvotes: 1

Views: 2080

Answers (3)

Senthuran
Senthuran

Reputation: 1837

I also had a same scenario. I had a sample spring boot application which uses the embedded H2 database. I've made a Dockerfile as below.

FROM openjdk:8
EXPOSE 8081
ADD target/book-rest-api-reactjs-0.0.1-SNAPSHOT.jar book-rest-api-reactjs-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/book-rest-api-reactjs-0.0.1-SNAPSHOT.jar"] 

Then I build the docker using the following command.

docker build -t book-rest-api-reactjs.jar .

Next list the images using the below command to ensure the image is available locally.

docker image ls

Then run the image using the below command.

docker run -p 9090:8081 book-rest-api-reactjs.jar

Now when I access the endpoint(http://localhost:9090/rest/books) i'm able to get the result.

Upvotes: 1

gtonic
gtonic

Reputation: 2313

For a more elegant way: in the past, the docker-maven-plugin worked best for me, sneak preview is here:

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>VERSION GOES HERE</version>
    <configuration>
        <imageName>example</imageName>
        <baseImage>java:8</baseImage>
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]   </entryPoint>
        <!-- copy the service's jar file from target into the root directory of the image --> 
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

Apart from the ability to build it right with your project, its also possible to push the image directly to a docker registry:

mvn ... docker:build -DpushImageTags -DdockerImageTag=latest -DdockerImageTag=tag

Upvotes: 1

Haoming Zhang
Haoming Zhang

Reputation: 2842

You were giving incorrect parameter in your build command. -t parameter used to tag the resulting image. Which means, -t should followed by a image name but not a jar file. You can find more details in docker build document.

Upvotes: 2

Related Questions