How to run a Spring Boot Application in Docker

  • Create an executable JAR — Spring Boot automatically does this thing using Spring Boot Maven plugin.
  • Create a Docker file — Docker file is a kind of a blueprint that contains all the setup and instructions to build your Docker Image.
  • Build Docker Image — We are building a Docker image by running the Docker File
  • Run docker container — Finally, run the Project in Docker

Create an executable JAR

$ mvn clean install
unittest-0.0.1-SNAPSHOT.jar file

Create a Docker File

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/unittest-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app. jar"]
  • “FROM openjdk:8-jdk-alpine” — It says that we are building our Docker Image from the base Image openjdk. So we are taking openjdk as a parent Image. we are building out an application on top of that.
  • “VOLUME /tmp” — This line is needed for application because Tomcat try to create a work folder inside this volume.
  • “ADD target/unittest-0.0.1-SNAPSHOT.jar app.jar” — Here we are adding our JAR which is inside out to target location to the Docker Image in the name of app.jar .
  • “ENTRYPOINT [“java”,”-jar”,”/app. jar”]” — This is for an entry point our application to run. This is what we are already done earlier(Run application using console/terminal). Here we are giving some instructions on how to run our application.

Build our Docker Image

$ sudo docker build -t <projectname in lowercase> <Docker File location>

Run Docker Container

Example :

sudo docker run -p8080:8080 springunittestapplication
 $ docker ps
$ docker container stop <Container ID>
$ docker container start <Container ID>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store