Dockerizing Spring boot application
Hi I’m going to give a quick guide in Dockerizing Spring boot app.
First lets see what Dockerizing is…
To understand this process of dockerizing we need to know about containers or application containerization
Application containerization (app containerization)
Application containerization is an OS-level virtualization method used to deploy and run distributed applications without launching an entire virtual machine (VM) for each app. Multiple isolated applications or services run on a single host and access the same OS kernel. Containers work on bare-metal systems, cloud instances and virtual machines, across Linux and select Windows and Mac OSes.
Currently docker is the most common containerization technology. Containerize application using Docker is simply known as Dockerizing or Dockerization.
Above is a basic description about what docker is. Now lets create a sample application and dockerize it.
First we need to download and install docker in to the local machine. In order to do this we need to log into docker-hub (if you don’t have a dockerId create an account in docker-hub and log into it) then we can download and install docker in to our local machines.
And now we need to create sample application. I’m gonna create a sample spring-boot application with rest endpoint to test the app. we can generate sample project from “SPRING INITIALIZR”.
Now we have a simple spring boot app. I’m gonna create a test endpoint in order to check when app is deployed in docker.
Now lets start Dockerizing, first we need to add the Dockerfile to the app
Dockerfile
We know that docker runs docker images inside docker containers. Dockerfile is use to build these images, Dockerfile is a text file containing all the commands and instructions we need to call in the terminal when we are creating the docker images.
Docker can build images automatically by reading the instructions from a
Dockerfile
. ADockerfile
is a text document that contains all the commands a user could call on the command line to assemble an image. Usingdocker build
users can create an automated build that executes several command-line instructions in succession.
For this example I going to use basic docker file. You can get full understand what we can do with this Docker file from here
Now I have created a Dockerfile for the app,
FROM —This instruct docker which base image is our image is based on and use that base image to build our image. Since I’m using java 8 I need my image to based on java 8 so I have added java 8 as the base image and docker will pull this from the public repository to my local machine to build the image. We can add more than one FROM in one Dockerfile and docker will create multiple images based on each base image.
VOLUME— The VOLUME
instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers.
ARG — The ARG
instruction defines a variable that users can pass at build-time to the builder with the docker build.
ADD — Adds a file or directory to the image.
ENTRYPOINT — This tells docker how to start app after the image is started.
Now I can build the docker image using the following command
docker build -t boot-docker-demo .
Now I’m going to use maven to build the docker image using the docker file instead of docker build for this we need to do few modifications to our pom.xml
First lets add docker-maven-plugin to the pom
docker-maven-plugin
You can use this plugin to create a Docker image with artifacts built from your Maven project. For example, the build process for a Java service can output a Docker image that runs the service.
In here I have config how I need to create my docker image. Image name with prefix and jar file.
Now We can create docker image using maven
mvnw install dockerfile:build
By default the plugin will try to connect to docker on localhost:2375. Make sure you have exposed the docker demon on localhost:2375 or we can set the DOCKER_HOST environment variable to connect elsewhere.
If you haven’t exposed the docker demon it will throw this error
Once we have exposed this and Dockerfile and pom file are updated once we run the mvnw install dockerfile:build docker will create the docker image
When build is running for the first time if we don’t have the base images in our local machine the build will take time to pull all the images.
Once we have base images in local machine the docker image will create in matter of seconds.
And now if I list all the docker images using docker images command we can see our base image and our images
To run our docker image we can use below command
docker run -p 8080:8080 -t your_docker_image_name
This will create new container and start docker image and run application on a Linux environment.
We can see our application running on a separate container.
To Run your docker images on spring profiles is as easy as passing an environment to the docker run command.
docker run -e "SPRING_PROFILES_ACTIVE=prod" -p 8080:8080 -t image_name
As a plus I’m gonna show you guys a tool that we can use to manage our docker environments. This is called portainer
We can install portainer to our local machine using following command
Once we start this portainer will be running on the given port on localhost
This can be used to easily manage our docker images, containers. Read more about this on here
And for the full implementation of dockerizing spring boot applications can be found in github