Deploy Java Spring server with Docker container

June 02, 2016

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://p80.pool.sks-keyservers.net:80 — **recv**-**keys** 58118E89F3A912897C070ADBF76221572C52609D

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

    deb [https://apt.dockerproject.org/repo](https://apt.dockerproject.org/repo) 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 (https://hub.docker.com/) 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
    VOLUME /tmp
    ADD target/fleet-beacon*.jar app.jar
    EXPOSE 8080
    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/](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.


Profile picture

Experience in software development, application architecture, and deploying cloud solutions for enterprise customers. Strong hands-on skills with a Master's degree in Computer Science and business acumen with a master of business administration (MBA) in Finance. Certified in Amazon Web Services (AWS), Google Cloud Platform (GCP), Microsoft Azure, Kubernetes (CKA, CKAD, CKS, KCNA) and Scrum (PSM, PSPO) with experience in building banking products from scratch. Connect on Linkedin

© 2022, @victorleungtw