Docker : Spring boot with docker

In the previous post we saw docker basic concepts and installation. Here we will develop a spring boot app and create an image of it and run it on docker. I will be using maven as a build tool for the same. The final project structure would be like:


Lets start with our pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


<description>Demo project for spring-boot-docker-start</description>






Notice the docker-maven-plugin. The configuration specifies 3 things:

  1. The image name (or tag), which will end up here as spring-boot-docker/spring-boot-docker-start
  2. The directory in which to find the Dockerfile
  3. The resources (files) to copy from the target directory to the docker build (alongside the Dockerfile) – we only need the jar file in this example

Rest all configuration is what a typical spring basic web app stuffs.

Launcher class

package com.mynotes;

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

public class Application {

public static void main(String[] args) {, args);

Simple controller

package com.mynotes;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class HomeController {

private Logger logger = LogManager.getLogger(this.getClass());
public String home(){
String msg ="Congratulations! Docker app deployed successfully!";;
return msg;


Now we’ll have to set up our own Docker image. Docker has a simple Dockerfile file format that it uses to specify the “layers” of an image. To do that you create a fine named Dockerfile. I placed mine inside the src/main/docker folder

FROM java:8
<code class="plain">EXPOSE </code><code class="value">8080</code>
ADD spring-boot-docker-start.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-jar","/app.jar"]

Lets see each line step by step :

  • FROM java:8 – This is our base image or first layer
  • EXPOSE 8080 - port number our app will be started
  • ADD spring-boot-docker-start.jar app.jar – The project JAR file is ADDed to the container as “app.jar” and then executed in the ENTRYPOINT.
  • RUN sh -c ‘touch /app.jar’ – touch our file so it has modification time (Docker creates all container files in an “unmodified” state by default)
  • ENTRYPOINT [“java”,”-jar”,”/app.jar”] –  execute our fat jar

You can build a tagged docker image like:

mvn package docker:build

Notice the build :


Lets see the images using ‘docker images‘ and run it

docker run -d -p 3000:8080 spring-boot-docker/spring-boot-docker-start

Above we used -d to run in demon mode. Now the app is running on 8080 in the container which is isolated. We have to expose. We used -p for that. You can use the same or change the port as I did. Now the app is runing on 3000.


Checking browser


You can stop the app using ‘docker stop <id/name>‘ and then ‘docker rm <id/name>‘. Can remove image also using ‘docker rmi <id>



%d bloggers like this: