Spring, DigitalOcean, GitHub & Docker

Moritz Mitterdorfer
5 min readDec 19, 2020

Ever wanted to deploy a Spring app on a DigitalOcean droplet (server) so that it is accessible to all people around the globe? šŸŒ Creating such cloud-solutions is not hard!

  1. Creating the App šŸ–„
  2. Containerizing it (Docker) šŸ³
  3. Publishing code to GitHub šŸ„³
  4. Deploying it to server ā˜ļø

You can find the code for this guide here: https://github.com/momtr/spring-docker-digitalocean :) The guide is beginner-friendly, but depending on your knowledge, you may skip several parts of it. So, letā€™s get started!

The App

Letā€™s start by creating the API! Therefore, we use Spring Initializr (https://start.spring.io/) for setting things up. Just click on the link and enter your preferred settings. Weā€™ll use Java 8 & Maven. Weā€™ll also need a dependency: Spring Web (add it via clicking on ā€˜Add Dependencyā€™).

Spring Initializr

Download the project (click ā€˜Generateā€™) and move it into your preferred directory. Then, weā€™re ready to open it with IntelliJ IDEA! šŸŽŠ

Opening the project might take a few seconds. Next step: writing the controller that manages client requests.

Whatā€™s a controller? Rest controllers manage client requests and return a response. For instance, some user might request /hello and the controller is the part of the application that manages this request and returns ā€œhello worldā€.

Letā€™s say we want to write a controller that returns a random number. The request will hit the ā€˜/random/numbersā€™ endpoint. Therefore, we simply create a package called controller and create our first controller called RandomNumberController in it.

We also annotated the controller class with @RestController to tell Spring that it is a controller. Then, we are ready to write our method (annotated with @GetMapping). The method just returns a random double. Perfect :)

screenshot made with carbon.now.sh
Screenshot made with https://carbon.now.sh

Now, weā€™re ready to run the app locally on the computer. Go into AppApplication and run the main method. Then, youā€™ll see the logs of the application.

Testing the app is also quite easy! Go into your browser and hit http://localhost:8080/random/numbers. One thing you might have noticed is, that our application runs on port 8080 on the computer.

Wuhuu, it works!! šŸ„³ Now, we want to dockerize that app.

Docker Magic

Dockerizing the app, basically ensures that the app runs everywhere where Docker is installed. If you did not install Docker so far, have a look at that page: https://docs.docker.com/get-docker/.

Next, we create a Dockerfile in the Spring app. This file includes all the necessary steps to run the application and to create the appā€™s Docker image.

We basically have two stages: In the build stage, we download maven and install all necessary dependencies based on our pom.xml file. In the packaging stage, we install java, copy all files into the container and run the app. Quite easy, huh? Now, weā€™re done with creating the app. What the project should look like:

App

GitHub

As we want our code to be accessible by some server, we create a new GitHub repo and upload the code ā€” we somehow have to transfer the code to our server. If you donā€™t have a GitHub account, sign up ā€” itā€™s free!

GitHub repo
GitHub repo

To publish the code to GitHub, open a terminal (or CMD) in the directory of your app. First, we need to initialize the directory with Git, add the remote origin (creating reference to GitHub repo) and staging and committing our code.

To push it to the remote repo, we have to do git push ā€” set-upstream origin master.

Now, you can view your project on GitHub:

Deploying

Now, we are ready to deploy everything! Register for a DigitalOcean account and add a Droplet to your account. This may come at a price, based on your settings. But, DigitalOcean often provides 100$ in credits when signing up, which is awesome.

Everything you need is is the IP address, the username and the password of your droplet. Create an SSH connection via your terminal: ssh username@ip_address

Next, we clone the repository from GitHub by saying: git clone repo_url (repo_url is the URL of your repo, which you can find out by visiting you repo -> click Code). Then, we cd into the repo. Now, the code is on the droplet (server).

Finding our the repoā€™s HTTPS clone URL
Terminal (SSH server connection)

Finally, we can start the app! First, we need to build the appā€™s image, which may take a few seconds as Docker must run all commands included in the Dockerfile.

Building the image

Finding out the imageā€™s ID (which weā€™ll need for the final step) is easy. The docker images command lists all images, including the one weā€™ve just created.

Copy its ID and run the following command: docker run -p 8080:8080 feb634965069 where the last part is the ID of the image. Now, the app has been deployed and is accessible to everyone! šŸ„³šŸŽŠ

Conclusion

Congrats! šŸš€ Youā€™ve deployed a Spring app that is capable of returning a random number. To summarize what you just did:

  • creating a Spring app with Spring Initializr and writing the code for the controller
  • creating a Dockerfile
  • publishing the code to GitHub
  • creating an SSH connection to the server and cloning the GitHub repo
  • building the Docker image from the Dockerfile and running the image

--

--