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!
- Creating the App š„
- Containerizing it (Docker) š³
- Publishing code to GitHub š„³
- 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ā).
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 :)
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:
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!
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).
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.
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