Deploy Java Spring server with Docker container

I am deploying a Java Spring server using Docker. Here are the steps:

1. Launch a ubuntu server

In this demo, assume you have a server launched with ubuntu 14.04. Install docker via APT repository:

$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates
$ sudo apt-key adv — keyserver hkp:// — recv-keys 58118E89F3A912897C070ADBF76221572C52609D

Open /etc/apt/sources.list.d/docker.list with your favorite editor and add this line:

deb ubuntu-trusty main

Install docker in the server as follow:

$ sudo apt-get update
$ sudo apt-get install docker-engine
$ sudo service docker start

2. Build Docker image

Login to Docker hub ( and create a new repository. Then in your terminal,

$ docker login

with your username and password.

In your local development Java spring folder, create a Dockerfile like this:

FROM frolvlad/alpine-oraclejdk8:slim
ADD target/fleet-beacon*.jar app.jar
RUN sh -c ‘touch /app.jar’
ENTRYPOINT [“java”, “-jar”,”/app.jar”]

And build the image:

$ docker build -t username/repo-name .

*where -t stands for tag, 
*replace username and repo-name with your namespace. 
*also don’t forget the dot .

Push the image to your remote repository:

$ docker push username/repo-name

3. Pull the docker image

In your remote ubuntu server, docker login and pull the image:

$ docker pull username/repo-name

Run it in the background

$ docker run -d -p 8080:8080 username/repo-name

*where -d means detached, 
*-p means publish all exposed ports to the host interfaces, for example 8080 to 8080.

4. Nginx setup

With the vim editor, open the file in /etc/nginx/sites-available/default, and edit as follow

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
 root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/;

Exit and save with :wq! that’s it. Open a browser and point to your remote server IP, you should be able to see the Java Spring page running.

5. Troubleshooting

Sometimes if you encounter a problem with the daemon connection:

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

Run the command:

$ eval $(docker-machine env default)

If your are testing locally and couldn’t find your IP address, try this command to find the IP:

$ docker-machine ls

Leave a comment below if you encounter other issues as well.

By Victor Leung

Experience in software development, consulting services and technical product management. Understanding of business and technology with an MBA in Finance and a Master degree in Computer Science. AWS Certified Solution Architect with experience in building products from scratch and serving as a charismatic leader.

Leave a comment

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: