Deploying a Java Spring Server with a Docker Container

June 02, 2016

In this guide, I'll show you how to deploy a Java Spring server using Docker. Below are the steps to follow:

1. Launch an Ubuntu Server

For this demo, let's assume you have launched a server running Ubuntu 14.04. Install Docker using the APT repository:

$ sudo apt-get update  
$ sudo apt-get install apt-transport-https ca-certificates  
$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D  

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

deb [https://apt.dockerproject.org/repo](https://apt.dockerproject.org/repo) ubuntu-trusty main  

Proceed to install Docker on the server:

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

2. Build the Docker Image

Log in to Docker Hub (https://hub.docker.com/) and create a new repository. Then, in your terminal, run:

$ docker login  

Enter your username and password when prompted.

In your local development Java Spring folder, create a Dockerfile with the following content:

FROM frolvlad/alpine-oraclejdk8:slim  
VOLUME /tmp  
ADD target/fleet-beacon*.jar app.jar  
EXPOSE 8080  
RUN sh -c 'touch /app.jar'  
ENTRYPOINT ["java", "-jar", "/app.jar"]  

To build the image, execute:

$ docker build -t username/repo-name .  

Here, -t stands for "tag." Replace username and repo-name with your Docker Hub username and repository name. Also, don't forget the trailing dot.

Push the built image to your remote repository:

$ docker push username/repo-name  

3. Pull the Docker Image

On your remote Ubuntu server, log in to Docker and pull the image:

$ docker pull username/repo-name  

Run the container in the background:

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

Here, -d means "detached," and -p specifies that all exposed ports (e.g., 8080) should be published to the host interfaces.

4. Set Up Nginx

Using the Vim editor, open /etc/nginx/sites-available/default and modify it as follows:

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/](http://localhost:8080/);
  }
}

Exit and save with :wq!.

That's it! Open a browser and navigate to your remote server's IP address; you should see the Java Spring page running smoothly.

5. Troubleshooting

If you encounter an issue with the Docker daemon connection, showing:

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

Run the following command:

$ eval $(docker-machine env default)  

If you're testing locally and can't find your IP address, use this command to find it:

$ docker-machine ls  

Feel free to leave a comment below if you encounter any other issues.


Profile picture

Software development professional with expertise in application architecture, cloud solutions deployment, and financial products development. Possess a Master's degree in Computer Science and an MBA in Finance. Highly skilled in AWS (Certified Solutions Architect, Developer and SysOps Administrator), GCP (Professional Cloud Architect), Microsoft Azure, Kubernetes(CKA, CKAD, CKS, KCNA), and Scrum(PSM, PSPO) methodologies. Happy to connect