Dockerizing Spring Web Application

Using Docker to ship Spring Boot web application and PostgresSQL DB

By Tomche Delev / @tdelev


  • What the heck is Docker?
  • How the containers changed the world?
  • Docker intro
  • Ship the containers in six steps

What is Docker?

According the official docs Docker is a platform that can be used to



run applications


How do you ship your application?

How were the goods transpored?

How are the goods transpored NOW?

The container has become the standardized API of cargo.

Just as the phisical ship containers are used to tranport goods, the virtual container can be used to ship and deploy any service

VM image

Downsides of VM aproach are:

  • no guaranty of virtualized envirnoment
  • heavyweight technology
  • slow and IO intensive building and starting process
  • deployment costs on IaaS such as AWS EC2

Docker introduction

Docker is becomingly

increasingly popular

Allows you to package a microservice in a

standardized portable format

that’s independent of the technology used to implement the service

Provides a

high degree of isolation

between different services

Docker containers are

extremely lightweight

and as a result can be built and

started extremely quickly

It runs natively on


You can also run Docker on Windows and Mac OSX using


Some clouds also have added extra support for Docker

The two main Docker concepts are


which is a portable application packaging format


which is a running image and consists of one or more sandboxed processes

Docker images

A Docker image is read-only file system image of an operating system and an application.

An image is


and will run on any Docker installation

You can create an image from scratch

but normally an image is created by starting a container from

existing base image

installing applications by executing the same kinds of commands you would use when configuring a regular machine

apt-get install –y

and then saving the container as a new image

For example, to create an image containing a Spring Boot based application, you could start from a

vanilla Ubuntu image

install the JDK

and then

install the executable JAR

Docker containers

A Docker container is a running image consisting of one or more sandboxed processes

What we get from Docker containers?


  • process groups
  • namespaces
  • own root file-system

Docker container often only consists of the application’s processes

Resource limits

  • memory
  • CPU

Networking isolation

Spring Boot and Docker

Let’s now build a Docker image that runs the Spring Boot application

Step 1

Create the image Dockerfile

FROM java:8
COPY demo-prod.jar /apps/spring_app/
WORKDIR /apps/spring_app
CMD ["java", "-jar", "demo-prod.jar"]

Step 2

Build the image

$ docker build -t tdelev/spring_app .

Step 3

Pull database image

$ docker pull postgres

Step 4

Create data volume

$ docker create -v /var/lib/postgresql/data \
--name spring_app_data postgres:9.4

Step 5

Start database instance

docker run --volumes-from spring_app_data \
--name spring_app_pg \
-e POSTGRES_USER=demouser -e POSTGRES_PASSWORD=demopass \
-d -P postgres:9.4 \

To connect to the database execute

docker run -it --link spring_app_pg:postgres \
--rm postgres sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" \
-p "$POSTGRES_PORT_5432_TCP_PORT" -U demouser' \

create database demo;

Step 6

Run app

docker run --name spring_app_container \
--link spring_app_pg:spring_app_pg \
-p 8080:8080 \
-d tdelev/spring_app

Connect to database instance

psql -h localhost -p 49157 -U demouser --password

Your reaction?


Thank You