Run SpringBoot Application Inside Docker Container

Vimit Dhawan
3 min readOct 24, 2020

Goal:

The goal of this article is to run a spring boot application inside the docker. I am going to publish a series of articles on running Full Stack, Microservices, Kafka, Cassandra inside the docker.

Prerequisite:

Basic understanding of SpringBoot, Docker

Setup Docker:

You have to go through this link Docker Just follow the steps mention in this link. In the end, you can open the terminal and run the command docker — version.

Setup SpringBoot:

You have to go through this link SpringBoot. you have to fill in the Group, Artifact, and other required details and click on the generate button. After that, you have to import the project in IntelliJ or Eclipse.

I am assuming that you know the basics of setup the spring boot application and rest endpoint.

Docker Image With JDK:

As we know that Java application needs JDK. So let’s use the existing docker image for JDK. You have to run the below command

docker run -dit openjdk:8-jdk-alpine

-dit means that we are running this command in detach and interactive mode. so detach means your terminal will be free after running this command and interactive means you can run more commands inside the container.

After that run the below command docker images

Create Image With SprinBoot:

Now you have to open your project and run the goal mvn clean package. After running this command.

Now you can see your application Jar inside the target folder. For me it’s hello-world-rest-api.jar. But for you, it’s different depends upon your application setting.

Now let’s copy this jar inside our docker container. In the terminal, you have to go to the application path and run the below command. So naughty_knuth is the name of docker container it’s different for you.

docker container cp target/hello-world-rest-api.jar naughty_knuth:/tmp

You have to run the command docker container ls

As you can see for me it’s recursing_shamir.

Now let’s commit this image with a copy Jar. You have to run the below command

docker container commit --change='CMD ["java","-jar","/tmp/hello-world-rest-api.jar"]' recursing_shamir hello-world-rest-api:manual2

As you can see — change parameter has a command which will run when we are going to pull this new image. Now we are renaming recursing_shamir with a new image name hello-world-rest-api:manual2 . This new image has our jar and starts command.

Run Image:

Now we have to run the below command to run this image and you will see our application is running inside the docker.

docker run -p 8080:8080 hello-world-rest-api:manual2

Validate:

After running the above command you can go to your browse and run http://localhost:8080/hello-world

hello-world is the rest endpoint in my application so you can search the one that you created in your application.

Please comment on this article to suggest to me how can I improve in future articles. In case you face any issue in your setup. Please comment on this article I will try to help.

--

--

Vimit Dhawan
0 Followers

Working as a Software Developer