First look: Spring Boot and Docker

Everyone has eared that Spring Boot is the faster way to build applications. But, what everyone is trying to find right now is: What is the simplest and faster way to deploy applications?

Amazon Web Services has announced in AWS re:Invent 2014 the new Amazon EC2 Container Service that supports Docker containers and allows you to easily run distributed applications on a managed cluster of Amazon EC2 instances. This have created a lot of buzz around Docker.

Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud.

The following code sample is to show how to have an Spring Boot application deployed using Docker.


All the code can be download from my github account:


Install Docker in your local machine, follow one of this guides:

  1. Directory Structure

    Only 3 files are needed: Java Class, pom.xml and Dockerfile.

  2. Dependency Library

    Using Spring Boot parent pom really help with all the dependencies management. We only have two dependencies:

    • spring-webmvc – The standard dependency from Spring Framework to create a Web Application.
    • spring-boot-starter-web – The mighty and glorious, Spring Boot.
  3. Build Plugin Library

    The plugins for build are only two:

    • spring-boot-maven-plugin – This will enable us to start the web application with: java -jar spring-boot-maven-docker.jar
    • maven-resources-plugin – We need this one to copy our Dockerfile to the target directory, close to the spring-boot-maven-docker.jar
  4. Application Controller

    Using Spring boot can take really help to start a web application without the overhead of all the old Spring Framework XML configurations, the configurations of Servlets for the Servlet Container web.xml – Now, you can simply use the EnableAutoConfiguration annotation and a simple main method.

    package com.adaofeliz;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
     * Created on 21/11/14.
    public class ApplicationController {
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String helloWorld() {
            return "Hello World: Spring-boot Sample Application using Maven and Docker";
        public static void main(String[] args) throws Exception {
  , args);
  5. Dockerfile

    Very easy to read, the Docker configuration is really simple.

    FROM dockerfile/java:oracle-java7
    ADD spring-boot-maven-docker.jar /opt/spring-boot-maven-docker/
    EXPOSE 8080
    WORKDIR /opt/spring-boot-maven-docker/
    CMD ["java", "-jar", "spring-boot-maven-docker.jar"]
  6. Test

    Clone this project

    git clone [email protected]:adaofeliz/spring-boot-maven-docker.git spring-boot-maven-docker

    Build Application

    cd spring-boot-maven-docker
    mvn clean install
    cd target
    docker build -t spring-boot-maven-docker .


    Check Image Id

    docker images


    Start Docker container with the previous Image Id

    docker run -p 8080:8080 <image id>


    Open your browser: http://localhost:8080

5 thoughts on “First look: Spring Boot and Docker”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s