Running Spring Boot in A Docker Container

Running Spring Boot in A Docker Container


Containers based deployments are rapidly gaining popularity in the enterprise. One of the more popular container solutions is Docker.

Many view containers as virtual machines. They’re not. Well kind of not. A container is a virtual walled environment for your application. It’s literally a ‘container’ inside the host OS. Thus your application works like it is in it’s own self contained environment, but it’s actually sharing operating system resources of the host computer.  Because of this, containers are more resource efficient than full blown virtual machines. You get more bang for your buck running a bare metal machine with a bunch of containers, than you do running a bare metal machine with a bunch of VMs. This is why massive cloud computing companies running 10’s of thousands of servers are running containers. Google, Facebook, Netflix, Amazon are all big advocates of containers.

Introducing  Docker Containers

To help you visualize the difference, here are a couple images provided by Docker. Here is the bloated architecture of a traditional virtual machine environment. A popular solution you can try is Oracle’s Virtual Box which allows you run a variety of operating systems on your personal machine. I personally use VMWare Fusion to run Windows on my MBP (and I still feel a little dirty every time I do). If you’ve never used either these, I recommend you give them a try.

In this graphic, note how each stack has its own Guest OS.


Now for comparison, here is the same stack containerized by Docker. Here you can see how each application does not get its own operating system. This is key to why Docker containers are so efficient. You’re not providing a virtual layer mimic the hardware, for the the guest OS to use. And you’re not running n+1 guest hosts either.

Docker What is a VM

Clearly this is more efficient computing. I’ve seen estimates in the 10-25% range of improved performance. But like with everything else when it comes to computing performance – your mileage may vary. I’d expect lightweight linux VMs to be closer to the 10% side of the scale, and Windows VMs probably closer to the 25% end of the scale – just because the Windows OS is so bloated in comparison.

This leads me to an important distinction about Docker – Linux only. Yes, you can “run” Docker on Windows and OSX – but at this time, you can only do so using a VM running in Virtual Box to run – a Linux VM.

Running Spring Boot in a Docker Container


When I first heard about running Spring Boot in a Docker container, I personally thought – “now why would you want to run a JVM in a VM, on a VM?” At first glance, it just seemed like a absolutely terrible idea from a performance standpoint. I doubt if any of these solutions will ever match the performance of a JVM running on a bare metal installation of Linux. But, I’ve shown above, running a Spring Boot application in a Docker container should have minimal performance impact. Certainly less of an impact than running in a VM. Which is exactly what you’re doing running applications in any cloud provider (see image one above).

Installing Docker

I’m not going to get into installing Docker on your OS. There is ample documentation about installing Docker in the internet. Going forward, I’m going to assume you have Docker installed. Since Docker is Linux based, my focus will be on Linux (RHEL / CentOS).

Introduction to Docker CourseSpring Boot Example Application

For the purposes of this tutorial, let’s start with a simple Spring Boot Application. I’m going to use the completed application from my Mastering Thymeleaf course. This is a simple Spring Boot web application which is perfect for our needs.

If you want to follow this tutorial along step by step, head over to GitHub and checkout this Spring Boot project. Be sure to change to the branch “spring-boot-docker-start”.

Building a Spring Boot Docker Image

For us to run Spring Boot in a Docker container, we need to define a Docker image for it. Building Docker images is done through the use of “Dockerfile”s. Dockerfiles are basically a manifest of commands we will use to build and configure our docker container. To configure our Docker image to run our Spring Boot application, we will want to:

  • Start with the latest CentOS image from Docker Hub.
  • Install and configure Oracle Java.
  • Install the Spring Boot artifact – our executable JAR file.
  • Run the Spring Boot application.

I’m using CentOS for its compatibility with RHEL, which is probably the most popular linux distribution used by enterprises. And Oracle’s Java, mainly for the same reason.

Create Our Dockerfile

In our Maven project, we need to create our Dockerfile. In /src/main/docker  create the file Dockerfile .

NOTE: As a Java developer you may be tempted to create the file as DockerFile. Don’t do this. The Maven Plugin we cover later won’t see your file if it’s CamelCase. I learned this lesson the hard way.


We’ll start our Docker image off by using the CentOS image from Docker hub.


Installing Oracle Java

The following lines in our dockerfile will install wget into the image using the yum package installer, download the Oracle Java JDK from Oracle using wget, then configure Java on the machine.


Installing the Spring Boot Executable Jar

In this section of the Dockerfile, we are:

  • Adding a /tmp volume. Docker will map this to to /var/lib/docker on the host system. This is the directory Spring Boot will configure Tomcat to use as its working directory.
  • The ADD  command adds the Spring Boot executable Jar into our Docker image.
  • The RUN  command is to ‘touch’ the JAR and give it a modified date.
  • The ENTRY  point is what will run the jar file when the container is started.

I learned about these configuration settings from a post from the Pivotal team here.


Complete Dockerfile

Here is the complete Dockerfile.


Building the Docker Image Using Maven

Naturally, we could build our Docker image using docker itself. But this is not a typical use case for Spring developers. A typical use case for us would be to use Jenkins to generate the Docker image as part of a CI build. For this use case, we can use Maven to package the Spring Boot executable JAR, then have that build artifact copied into the Docker image.

There’s actually several competing Maven plugins for Docker support. The guys at Spotify have a nice Maven / Docker plugin. In this example, I’m going to show you how to use the Fabric8 Docker plugin for Maven.


Of the Maven plugins for Docker, at the time of writing, Fabric8 seems to be the most robust. For this post, I’m only interested in building a Docker Image for our Spring Boot artifact. This is just scratching the surface of the capabilities of the Fabric8 Maven plugin. This plugin can be used to spool up Docker Images to use for your integration tests for CI builds. How cool is that!?!? But let’s learn to walk before we run!

Here is a typical configuration for the Fabric8 Maven plugin for Docker.

Fabric8 Maven Docker Plugin Configuration

If you’re following along in tutorial, the complete Maven POM now is:


Building the Docker Image

To build the Docker image with our Spring Boot artifact run this command:

The ‘clean’ tells Maven to delete the target directory. While this step is technically optional, if you don’t use it, sooner or later you’re going to get bit in the ass by some weird issue. Maven will always compile your classes with the package command. If you’ve done some refactoring and changed class names or packages, without the ‘clean’ the old class files are left on the disk. And in the words of IBM – “Unpredictable results may occur”.

It is very important to run the package command with the docker:build command. You’ll encounter errors if you try to run these in two separate steps.

While the Docker image is building, you will see the following output in the console:

Docker images are built in layers. The CentOS image from Docker Hub is our first layer. Each command in our Dockfile is another ‘layer’. Docker works by ‘caching’ these layers locally. I think of it as being somewhat like your local Maven repository under ~/.m2. Where Maven will bring down Java artifacts once then cache them for future use.

The first time you build this Docker image, will take longer since all the layers are being downloaded / built. The next time we build this, the only layers that change are the one which adds the new Spring Boot artifact, all commands after this. The layers before the Spring Boot artifact aren’t changing, so the cached version will be used in the Docker build.

Running the Spring Boot Docker Image

Docker Run Command

So far, we have not said anything about port mapping. This is actually done at run time. When we start the Docker container, in the run command, we will tell Docker how to map the ports. In our example we want to map port 8080 of the host machine to port 8080 of the container. This is done with the ‘-p’ parameter, followed with <host port>:<container port>. We also want to use the ‘-d’ parameter. This tells Docker to start the container in the background.

Here is the complete command to run our docker container:

This command will start the Docker container and echo the id of the started container.

Congratulations, your Spring Boot application is up and running!

You should now be able to access the application on port 8080 of your machine.

Working with Running Docker Containers

Viewing Running Docker Containers

To see all the containers running on your machine, use the following command:

View Log Output

Our running Docker containers are far from little black boxes. There’s a lot we can do with them. One common thing we want to do is see the log output. Easy enough. Use this command:

Access a Running Docker Container

Need to ssh into a Docker container? Okay, technically this really isn’t SSH, but this command will give you a bash:

Stopping the Docker Container

Shutting down our Docker container is easy. Just run this command:

Ending Source Code

Just in case you’ve run into trouble, like always, I have a branch in GitHub with the complete working example. You can get the ending source code for this tutorial here on GitHub.


The default executable Jar artifact of Spring Boot is ideal for deploying Spring Boot applications in Docker. As I’ve shown here launching a Spring Boot application in a Docker container is easy to do.

In terms of technology, Docker is still fairly young. At the time of writing, Docker is only about three years old. Yet, it is rapidly catching on. While Docker is widely used by the web giants, it is just starting to trickle down to Fortune 500 enterprises. At the time of writing, Docker is not available natively on OSX or Windows. Yet. Microsoft has committed to releasing a native version of Docker for Windows. Which is interesting. A lot things going on around Docker at Red Hat and Pivotal too.

Docker is a fundamental paradigm shift in the way we do things as Spring developers. I assure you, if you’re developing applications in the enterprise using the Spring Framework and have not used Docker, it’s not a matter of if, it is a when.

As a developer, Docker brings up some very cool opportunities. Need a Mongo database to work against? No problem, spool up a local Docker container. Need a virtual environment for your Jenkins CI builds. No problem.

I personally have only been working with Docker a short time. I am honestly excited about it. My thoughts on Docker – Now we’re cooking with gas!


About jt

    You May Also Like

    23 comments on “Running Spring Boot in A Docker Container

    1. September 27, 2016 at 6:31 am

      when i use website contact send message, she tell me Failed to send your message. Please try later or contact the administrator by another method.

    2. September 27, 2016 at 6:32 am

      Why does Spring Boot web app close immediately after starting?
      I’ve operation according to the article solves the problem, but I want to know why?

      please advise!


      • September 27, 2016 at 7:20 am

        Provided says the dep will be provided in the runtime environment, meaning if its not there, the app will fail on startup. Commenting it out, requires it to be included.

    3. October 19, 2016 at 4:19 pm

      Hi John, just want to say thank you for the nice article. I was struggling with building docker image with fabric 8 till i read important tip in this article: It is very important to run the package command with the docker:build command.
      Great work!

      • October 19, 2016 at 4:29 pm

        Glad it helped!!

    4. December 4, 2016 at 9:48 pm

      Hi John, thanks for this excellent blog. It is certainly the clearest article about building and running Spring Boot app in Docker I have read so far.

    5. March 16, 2017 at 2:01 pm

      Everything builds and runs but I notice when i do a “docker ps” but container is not there.

      • March 22, 2017 at 5:59 pm

        Sounds like the container terminated. Try docker ps -a and docker logs to see why

    6. June 16, 2017 at 10:18 am

      I have deployed the app under docker, and it started, the only confused thing is the image can not show up, any ideas?

      • June 16, 2017 at 10:46 am

        No, not sure what could cause that.

    7. June 29, 2017 at 11:54 pm

      I hadn’t thought of using containers but that’s a great idea. Thanks so much for sharing!

    8. July 3, 2017 at 2:12 pm

      Please consider extending this example to copy in a specific file. Would that be best done in the maven pom assembly section or somewhere else?

      • July 3, 2017 at 2:29 pm

        You can also specify an external properties file. I usually just override env specfic properties via environment variables – which will override values of

    9. August 11, 2017 at 4:32 pm

      I would like some clarification on a couple lines:
      1) java install. This URL no longer works. At this time, the only thing I can think of is “ADD” the java install from a previous download. Any thoughts on alternative solutions?
      2) I do not understand why the RUN alternative … commands are needed. Can someone explain this step.


      • October 3, 2017 at 5:32 pm

        That’s the Dockerfile I used to have a working spring boot app based on this article after some tuning:

        FROM centos:latest

        ENV JAVA_VERSION 8u144

        # Upgrading system
        RUN yum -y upgrade
        RUN yum -y install wget

        # Downloading & Config Java 8

        RUN wget –no-cookies –no-check-certificate –header “Cookie: oraclelicense=accept-securebackup-cookie” “$JAVA_VERSION-$BUILD_VERSION/090f390dda5b47b9b721c7dfaa008135/jdk-$JAVA_VERSION-linux-x64.rpm” -O /tmp/jdk-8-linux-x64.rpm
        RUN yum -y install /tmp/jdk-8-linux-x64.rpm
        RUN alternatives –install /usr/bin/java jar /usr/java/latest/bin/java 200000
        RUN alternatives –install /usr/bin/javaws javaws /usr/java/latest/bin/javaws 200000
        RUN alternatives –install /usr/bin/javac javac /usr/java/latest/bin/javac 200000

        EXPOSE 8081

        VOLUME /tmp
        ENV JAVA_HOME /usr/java/latest
        ADD /target/price-service.jar price-service.jar
        ENTRYPOINT [“java”,”-jar”,”price-service.jar”]

    10. September 6, 2017 at 4:54 pm

      I think we can make this little simpler. We may not need the centOS and can create the docker image using openJDk.

    11. September 30, 2017 at 10:23 am

      I love the post. Definitely the most straight forward tutorial around. I am having troubles getting Maven to build and run on my Mac. I’m new to docker so still pretty clumsy at present time.

      When I run: mvn clean package docker:build

      org.apache.maven.plugin.MojoExecutionException: Unable to check image [centos]

      org.apache.http.conn.HttpHostConnectException: Connect to [/] failed: Connection refused

      I’m guessing it’s because Docker isn’t running at but in a Virtual Linux env.

      • September 30, 2017 at 10:52 am

        yes – you’ll need to use the IP of the VM.

        • September 30, 2017 at 11:43 am

          I was able to get it working by following instructions from here:

          brew install socat
          socat TCP-LISTEN:2375,reuseaddr,fork,bind=localhost UNIX-CONNECT:/var/run/docker.sock

          But then I ran into this error:

          The command ‘/bin/sh -c wget –no-cookies –no-check-certificate –header “Cookie: oraclelicense=accept-securebackup-cookie” “$JAVA_VERSION-$BUILD_VERSION/jdk-$JAVA_VERSION-linux-x64.rpm” -O /tmp/jdk-8-linux-x64.rpm’ returned a non-zero code: 8

          • September 30, 2017 at 12:11 pm

            I tried openjdk and it built without errors:

            FROM openjdk:8

            EXPOSE 8080

            #install Spring Boot artifact
            VOLUME /tmp
            ADD /maven/sfg-thymeleaf-course-0.0.1-SNAPSHOT.jar sfg-thymeleaf-course.jar
            RUN sh -c ‘touch /sfg-thymeleaf-course.jar’
            ENTRYPOINT [“java”,””,”-jar”,”/sfg-thymeleaf-course.jar”]

    12. January 6, 2018 at 7:28 pm

      Thank you very much for the tutorial. It seems that the resource is no longer there on Oracle website.
      I encountered the problem below.

      Step 6/14 : RUN wget –no-cookies –no-check-certificate –header “Cookie: oraclelicense=accept-securebackup-cookie” “$JAVA_VERSION-$BUILD_VERSION/jdk-$JAVA_VERSION-linux-x64.rpm” -O /tmp/jdk-8-linux-x64.rpm
      —> Running in 2d6a43824aeb
      –2018-01-07 00:18:53–
      Resolving (…,
      Connecting to (||:80… connected.
      HTTP request sent, awaiting response… 302 Moved Temporarily
      Location: [following]
      –2018-01-07 00:18:53–
      Resolving (…, 2600:1407:21:28e::2d3e, 2600:1407:21:2ae::2d3e
      Connecting to (||:443… connected.
      HTTP request sent, awaiting response… 302 Moved Temporarily
      Location: [following]
      –2018-01-07 00:18:53–
      Connecting to (||:80… connected.
      HTTP request sent, awaiting response… 404 Not Found
      2018-01-07 00:18:54 ERROR 404: Not Found.

    13. January 8, 2018 at 10:27 pm

      Hello, congratulation by article,
      I’m think if would be possible to parametrize the line: ADD /maven/springboot-docker.jar app.jar ?

      Something like that with: “ADD /maven/${ARG_FILEJAR_NAME}.jar app.jar”

      • January 9, 2018 at 7:09 am

        Yes, this is possible in more recent versions of the Fabric8 plugin.


    Leave a Reply