Spring, DigitalOcean, GitHub & Docker

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 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:



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:


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! 🥳🎊


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

16, Vienna :))

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store