by BehindJava

How To Deploy a Spring Boot Application in Docker

Home » springboot » How To Deploy a Spring Boot Application in Docker

In this tutorial we are going to learn about deploying the spring boot application in Docker.

Deploy a Spring Boot Application

Open the command prompt in windows or terminal in Linux and check whether the docker is installed or not with the below command.

docker –version 

Now create a file called Dockerfile in your project folder and add the given below. In my case application is running on 8080 port number which is written in the Dockerfile as EXPOSE 8080.

After the mvn clean and build, jar file is generated under the target folder which written in the Dockerfile as ARG JAR_FILE=target/camel-ms-a-0.0.1-SNAPSHOT.jar where camel-ms-a-0.0.1-SNAPSHOT is the name of your application.

FROM adoptopenjdk/openjdk11:alpine-jre
ARG JAR_FILE=target/camel-ms-a-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Once we are ready with the Dockerfile in our project folder as shown below, we can open the command prompt from our project folder and type the below command.

mvn clean install

Once the build is success, Generate the docker build using below command.

docker build -f Dockerfile -t mydockerapp .

Run the Docker image generated from the above command and test the API’s from the browser or postman.

docker run -p 8080:8080 mydockerapp