Containerize Spring Boot Application using Docker
In this post we will deploy the simple Spring Boot Hello World application into Docker container.
Going through, we will see some other important concepts like how to go inside docker container and verify the images created.
To deploy the Jar file into container, we will use the docker own build form i.e. Dockerfile.
A Dockerfile is in principle, a linewise batch file, containing commands to build an image. It’s not absolutely necessary to put these commands into a file, because we’re able to pass them to the command-line, as well – a file is simply more convenient.
The following project can be cloned from official git repository in order to run the basic example of spring boot application inside a container.
$git clone https://github.com/spring-guides/gs-spring-boot-docker.git
Its a simple Hello world application. The code for the java file is as below :
This Dockerfile is very simple, but that’s all you need to run a Spring Boot app with no frills: just Java and a JAR file. The project JAR file is ADDed to the container as "app.jar" and then executed in the ENTRYPOINT.
Let's go through the each step to see what each command is doing :
FROM: The keyword FROM, tells Docker to use a given image with its tag as build-base. If this image is not in the local library, an online-search on DockerHub, or on any other configured remote-registry, is performed.
VOLUME: We added a VOLUME pointing to "/tmp" because that is where a Spring Boot application creates working directories for Tomcat by default. The effect is to create a temporary file on your host under "/var/lib/docker" and link it to the container under "/tmp". This step is optional for the simple app that we wrote here, but can be necessary for other Spring Boot applications if they need to actually write in the filesystem.
ADD: To add the jar in the docker container and rename it as app.jar
ENTRYPOINT : To reduce Tomcat startup time we added a system property pointing to "/dev/urandom" as a source of entropy.
There is a way to configure JRE to use a non-blocking entropy source by setting the following system property: -Djava.security.egd=file:/dev/./urandom
sudo docker build ./ --tag=springboot
You will have to run the docker command using sudo if you are not logged in as root user. docker build is the command provided by the docker , ./ is path from where the dockerfile is to be picked up.
--tag=springboot is the name of docker image that will be created.
sudo docker images
Above command will list down all the images and the latest created image will be there with the tag as latest and name given by you in the above command.
sudo docker run -p 8080:8080 -t springboot
Above command will run the docker images and expose port 8080 inside the
container where your spring boot application is running and map the external OS 8080 to the container port.
Once you run the above command , you will see the tomcat logs and your spring application logs on the terminal. To access the application in the browser enter url : http://localhost:8080.
This covers the concepts of deploying spring boot application on docker container. Please comment below , if you have any issue.
Going through, we will see some other important concepts like how to go inside docker container and verify the images created.
To deploy the Jar file into container, we will use the docker own build form i.e. Dockerfile.
A Dockerfile is in principle, a linewise batch file, containing commands to build an image. It’s not absolutely necessary to put these commands into a file, because we’re able to pass them to the command-line, as well – a file is simply more convenient.
Set up Spring Boot Application
The following project can be cloned from official git repository in order to run the basic example of spring boot application inside a container.
$git clone https://github.com/spring-guides/gs-spring-boot-docker.git
Its a simple Hello world application. The code for the java file is as below :
Create Dockerfile
Docker has a simple Dockerfile file format that it uses to specify the "layers" of an image.So lets create the docker file.
This Dockerfile is very simple, but that’s all you need to run a Spring Boot app with no frills: just Java and a JAR file. The project JAR file is ADDed to the container as "app.jar" and then executed in the ENTRYPOINT.
Let's go through the each step to see what each command is doing :
FROM: The keyword FROM, tells Docker to use a given image with its tag as build-base. If this image is not in the local library, an online-search on DockerHub, or on any other configured remote-registry, is performed.
VOLUME: We added a VOLUME pointing to "/tmp" because that is where a Spring Boot application creates working directories for Tomcat by default. The effect is to create a temporary file on your host under "/var/lib/docker" and link it to the container under "/tmp". This step is optional for the simple app that we wrote here, but can be necessary for other Spring Boot applications if they need to actually write in the filesystem.
ADD: To add the jar in the docker container and rename it as app.jar
ENTRYPOINT : To reduce Tomcat startup time we added a system property pointing to "/dev/urandom" as a source of entropy.
Tomcat entropy concepts :
Tomcat 7+ heavily relies on SecureRandom class to provide random values for its session ids and in other places. Depending on your JRE it can cause delays during startup if entropy source that is used to initialize SecureRandom is short of entropy. You will see warning in the logs when this happens, e.g.:
<DATE> org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [5172] milliseconds.
There is a way to configure JRE to use a non-blocking entropy source by setting the following system property: -Djava.security.egd=file:/dev/./urandom
Docker Build
We will run the below command build command to create the docker image using the above dockerfile.sudo docker build ./ --tag=springboot
You will have to run the docker command using sudo if you are not logged in as root user. docker build is the command provided by the docker , ./ is path from where the dockerfile is to be picked up.
--tag=springboot is the name of docker image that will be created.
List down the docker images
Above command will list down all the images and the latest created image will be there with the tag as latest and name given by you in the above command.
Run the docker image
Above command will run the docker images and expose port 8080 inside the
container where your spring boot application is running and map the external OS 8080 to the container port.
Once you run the above command , you will see the tomcat logs and your spring application logs on the terminal. To access the application in the browser enter url : http://localhost:8080.
This covers the concepts of deploying spring boot application on docker container. Please comment below , if you have any issue.
Comments
Post a Comment