Dockerizing Spring boot application

Dockerizing Spring boot applicationChamith KodikaraBlockedUnblockFollowFollowingJun 11Hi 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 containerizationApplication 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.

Enterprise Container Platform | DockerBuild, Share, and Run Any App, Anywhere.

Learn about the only enterprise-ready container platform to cost-effectively…www.


comDocker is a platform for developers and sysadmins to develop, deploy, and run applications with containers.

The use of Linux containers to deploy applications is called containerization.

Containers are not new, but their use for easily deploying applications isDocker DocumentationGet started with Docker Try our new multi-part walkthrough that covers writing your first app, data storage…docs.


comAbove 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”.

Spring InitializrInitializr generates spring boot project with just what you need to start quickly!start.


ioNow we have a simple spring boot app.

I’m gonna create a test endpoint in order to check when app is deployed in docker.

host:port/testNow lets start Dockerizing, first we need to add the Dockerfile to the appDockerfileWe 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.

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Using docker 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 hereBest practices for writing DockerfilesThis document covers recommended best practices and methods for building efficient images.

Docker builds images…docs.


comNow 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.

| Docker DocumentationDockerfile reference Docker can build images automatically by reading the instructions from a Dockerfile.

A Dockerfile…docs.


comNow I can build the docker image using the following commanddocker build -t boot-docker-demo .

The Docker image is createdNow 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.

xmlFirst lets add docker-maven-plugin to the pomdocker-maven-pluginYou 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 mavenmvnw install dockerfile:buildBy 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 errorOnce we have exposed this and Dockerfile and pom file are updated once we run the mvnw install dockerfile:build docker will create the docker imageWhen 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 imagesTo run our docker image we can use below commanddocker run -p 8080:8080 -t your_docker_image_nameThis 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_nameAs a plus I’m gonna show you guys a tool that we can use to manage our docker environments.

This is called portainerPortainer Management, Docker User Interface, Container Software – Auckland, Singapore, San…Build and manage your Docker environments with ease today.

Available on LINUX, WINDOWS, & OSX Portainer Community…www.


ioWe can install portainer to our local machine using following commandInstallation – Auckland, Singapore, San Francisco | Portainer.

ioRegister with Portainer By registering with Portainer we will be able to keep you up to date with a range of useful and…www.


ioOnce we start this portainer will be running on the given port on localhostThis can be used to easily manage our docker images, containers.

Read more about this on hereAnd for the full implementation of dockerizing spring boot applications can be found in githubChamithKodikara/boot-docker-demoSample application to demonstrate how to docker with spring boot – ChamithKodikara/boot-docker-demogithub.


. More details

Leave a Reply