Deploying a Java Spring Server with a Docker Container


Welcome to another episode of Continuous Improvement, the podcast where we explore tips and tricks for improving your development and deployment processes. I’m your host, Victor, and today we’re going to dive into the world of deploying a Java Spring server using Docker.

To start off, let’s assume you’ve already launched an Ubuntu server running Ubuntu 14.04. The first step is to install Docker on your server. Open up your terminal and follow these commands:

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

Next, we need to add the Docker APT repository to our sources list. Open up /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

Now, let’s proceed with the installation of Docker on our server:

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

Great! Now that Docker is installed, let’s move on to building our Docker image. First, log in to Docker Hub at https://hub.docker.com/ and create a new repository. Once that’s done, open up your terminal and run:

docker login

Enter your Docker Hub username and password when prompted.

Next, navigate to your local development Java Spring folder and create a file called Dockerfile. Inside the file, copy and paste 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"]

This Dockerfile sets up our Docker image with the necessary dependencies and configurations for running our Java Spring server.

Now, to actually build the Docker image, run the following command:

docker build -t username/repo-name .

Here, -t stands for “tag.” Make sure to replace username and repo-name with your Docker Hub username and repository name. Don’t forget the trailing dot at the end!

Fantastic! Our Docker image is built and ready to go. The next step is to push the image to your remote repository. Execute the following command:

docker push username/repo-name

This will push the image to your Docker Hub repository, making it accessible for deployment.

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

docker pull username/repo-name

This will ensure that the Docker image is available on your server.

With the image in place, it’s time to run the container. Execute the following command on your remote server:

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

The -d flag tells Docker to run the container in the background, and the -p flag specifies that port 8080 should be published to the host interfaces.

And just like that, your Java Spring server is up and running in a Docker container!

To complete the setup, we need to configure Nginx as a reverse proxy. Open up /etc/nginx/sites-available/default using the Vim editor. Modify the content 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/);
  }
}

Save the changes and exit the Vim editor.

And there you have it! Your Java Spring server is now successfully deployed using Docker and accessible through Nginx.

I hope you found this episode of Continuous Improvement helpful. If you encounter any issues or have any questions, feel free to leave a comment below the blog post. And remember, the key to continuous improvement is embracing new technologies and techniques. Thank you for listening and until next time, happy coding!