1

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?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.