Spring boot and docker


Spring boot has eased the life of developer by reducing boilerplate configuration and reduced project bootstrap time by declaring defaults. Those who are already working on Spring projects and are leveraging Spring boot must have realized productivity gains. In addition to that, Maven build script (pom.xml) has been shortened due to single dependencies made available by Spring boot for a specific purpose. For example including spring-boot-jpa-starter dependency in Maven script ensures that all the required dependencies for interacting with a database are available with a single declaration. While there are many benefits of spring boot, one of the major benefits is to run the application quickly with embedded tomcat instead of having need of a separate server. This has definitely shortened the development cycle and developers can now focus more on building solution for business requirements. Being able to easily deploy the application and that too with speed container approach is on the rise. Spring boot application can be easily be bundled along with container solutions like Docker.

Docker is one of the popular solutions for containerizing application and quickly spinning up the environment. Compared to the traditional approach of deploying an application on VM, docker offers flexibility and speed. The containerized applications are also immune to the VM problems and ensure that it will have the same behaviour across environments. In this article, we will take a look at how Spring boot application can be bundled in Docker. To get started following are the prerequisites for the example described below

  1. Java – Installer can be downloaded from Oracle
  2. Maven – Installation instruction can be found at Maven
  3. Eclipse – Installer can be downloaded from Eclipse
  4. Docker – Installation instruction for Windows, Linux, Mac

Once you are done with the installation, make sure that all the executable are added to the PATH variable of a respective OS. This will enable execution of the binaries from any location in your system.

Building Spring boot project
While there are multiple approaches to build a Spring boot project. The best place to get started is http://start.spring.io/. Enter following information in the form

Group – com.example.practice.spring.docker
artifact – springdockerhello
Dependencies – Web

Click Generate project button to download the artefact. This will download a zip file which contains Spring boot project with Maven script. Extract the project and import it in Eclipse as Existing maven project. Once the project is imported make sure that there are no errors.

You should now have following class in the project.

package com.example.practice.spring.docker.springdockerhello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringdockerhelloApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringdockerhelloApplication.class, args);

Notice the SpringBootApplication annotation. It does the magic of combining @configuration + @EnableAutoConfiguration + @ComponentScan.

Create Hello Spring Docker Service
Create new package as com.example.practice.spring.docker.springdockerhello.service, followed by new class HelloService. Refer to below code which creates simple hello world service.

package com.example.practice.spring.docker.springdockerhello.service;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class HelloService{

  public Message sayHello(){
    Message msg = new Message();
    msg.setGreeting("Spring boot rocks!!!");
    return msg;

  class Message {
    private String greeting;

    public String getGreeting(){return greeting;}
    public void setGreeting(String greeting){this.greeting = greeting;}


Now, run the SpringdockerhelloApplication class using Eclipse > Run As > Java Application. This will start embedded tomcat server and also publish REST service.

Open the browser and hit http://localhost:8080/hello You should now see a JSON response as

“greeting”:”Spring boot rocks!!!”

Now we have got our hello service working, let’s package it in Docker container. Create a new file under root project directory named as Dockerfile. Make sure the name exactly matches to Dockerfile.

Add following contents to the Dockerfile

FROM openjdk:8-jdk-alpine
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar", "/app.jar"]

Commands specified in Dockerfile are self-explanatory.

FROM – Specifies the base image to be used for building the docker image
ARG – Declares the build argument to be referenced
ADD – Adds file from the location specified to the given location
EXPOSE – Indicates the port to be exposed while running container
ENTRYPOINT – Indicate the default executable for the image. A nice explanation of ENTRYPOINT VS CMD is available here.

It’s time to build the docker image. The image represents application binaries bundled with container requirements to run the application. Execute the following command at the root of the project.

mvn package

docker build –build-arg JAR_FILE=target/springdockerhello-0.0.1-SNAPSHOT.jar -t spring-boot-docker .

The JAR_FILE is passed as build argument to docker build and it specifies the location from where the jar should be picked up. Once the build is completed you can verify whether image has been created by executing following command

docker image ls

Now we are ready to run our image as a container. Execute the following command to create container instance

docker run -p 8080:8080 spring-boot-docker

You should see spring boot log statements on the console. Once tomcat startup is complete. Open the browser and hit http://localhost:8080/hello You should see JSON response

“greeting”:”Spring boot rocks!!!”

The source code for this post is available at Github. In next post, we will look at how two docker containers – Spring boot and MySQL can communicate with each other.

Be Sociable, Share!

3 thoughts on “Spring boot and docker

  1. Sanket

    Good one yogesh. Awaiting your next blog. Database on docker will be intresting. Will be intresting to know how industry leverage db over containers

Leave a Reply

Your email address will not be published. Required fields are marked *