How to Run Spring-Boot App in Docker
A discussion of one of the most common looking and must-have skill for the folks who are working on Spring boot technologies and how to run app in container.
Join the DZone community and get the full member experience.Join For Free
Let's see in brief what is the purpose of running the spring-boot app in a container, we are hearing about modularising the application and making it a single independent service and then run this modular service, we don't need a huge computation machine to run the small service, we can leverage on the small computing piece called container which works based on OS concept namespace, where a small amount of computational power (CPU/Memory/Storage/Network) is provided from the underlying machine so that we can have a small dedicated computational power for multiple lightweight services where each service will run in a separate container.
- Spring Boot 2.3.x
- IDE STS/Eclipse
- Docker Configuration
Steps to Run Spring Boot Application in a Container
We will go over how we can run the spring boot application in the Docker container, sample spring boot project and docker files are here, clone the project if you want to try out running the same project in the container, the project also has a docker folder which contains Dockerfile and original jar file created from the spring boot application.
- Create any spring boot app of your interest, I have created an application which provides service for biker portal like listing the bikes, getting a specific bike, and deleting the bikes from the inventory.
- I have used SQLite DB here and didn't go with builtin H2Database, I want to show how we can manage and configure the dependencies on the container as the last piece of this discussion.
- Let's discuss the heart of this topic docker configuration, I am assuming you already installed the docker which manages the life cycle of docker-image else refer this for installing the docker, check the docker version with the following command
4. Here we will go over the docker image file, running an instance of the image will create the container which will have all the pre-configured procedure required to run the spring boot application, we need a platform to run the application here
- I have chosen centos and installed a Java and Sqlite on centos, RUN instruction allows you to install the required application and packages.
- Define a volume where all the configuration files need to be stored while running the image.
- Add/copy a jar jfilterdemo-0.0.1-SNAPSHOT.jar as myjfilter.jar to container
- Declare the entry point to configure a container(running instance of image) that will run as an executable, following is a command to run the jar file, this argument "Djava.security.egd=file:/dev/./urandom" is optional to make tomcat initialize and run faster,
- ENTRYPOINT has two forms:
ENTRYPOINT ["executable", "param1", "param2"](exec form, preferred)
ENTRYPOINT command param1 param2(shell form)
5. Let's go through steps to run and manage the docker image,
- Copy the above-created docker file and jar file to any directory of your wish or create a new directory, and run docker build command to build the docker image, "." indicates the current directory or we can specify the directory path where the Docker file present.
docker build -t spring-boot-docker.
- Now we will run the docker image that we have built in the last step, we need to mention on which port the app should run and listen and also need to map the running image to the external port to accept request from outside of the container.
docker run -d -p 8080:8080 spring-boot-docker
- Run the command "docker ps" to see what are all the images running inside a docker, we see the container id "b859b0ed334f", image name "spring-boot-docker" running on port 8080 and listening to port 8080 for an external request coming from outside of the container and docker will assign a random name to the container, in the below example "infallible_cori" is the name of the container
- Following is the command used to interact with the container or running image, you can pass the name of the container and the interactive mode, I have used bash mode here to open the command-line interface for the running image.
docker exec -it infallible_cori bash
- In the last step, we see how to enable the interactive mode, because from the docker file we have installed the SQLite DB but we haven't created a table/schema to persist the bike records, when we run the docker image the spring boot app will fail saying no table called bike found in SQLite DB. Enable command line and refer the following file to create a table in SQLite DB, steps to create a table in SQLite refer here.
- Now the final step to see interaction with up and running spring boot app, getting the list of bikes.
Opinions expressed by DZone contributors are their own.