Get Started
In this post, we will use Docker to create an image of a Spring Boot application and run it in a container.
Step 1 - Create a Spring Boot boilerplate app
Let’s create the application skeleton using the Spring Boot Initializer: http://start.spring.io,
including Web
as the only dependency and choosing Maven
(to follow the same instructions as in this post).
Step 2 - Code a simple REST Controller
Give your Spring Boot boilerplate project downloaded, just unzip it and edit the DemoApplication.java
file.
Then add a new controller to handle home using the following code:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
@RequestMapping("/")
public String home() {
return "Hello World";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Step 3 - Running the application locally
Use your local Maven installation or the wrapper (mvnw
command) normally included by the Spring initializer. From the
application’s root folder, execute:
$ ./mvnw spring-boot:run
This will fire up Tomcat server by default. To see your application in action, open a
browser window (or use command line tools like curl or httpie) and navigate to http://localhost:8080. You should see your Hello World
message:
As an alternative, you can also run the application by first packaging it in a jar file, and then run it with the java command. In that case, use Maven to create the package:
$ ./mvnw clean package
The resulting .jar
file will be placed in a new target
folder. Now you can execute this command and you will get the application running as before:
$ java -jar ./target/<your-file>-<version>/SNAPSHOT.jar
So far there is nothing to do with Docker, but it’s important to highlight a couple of concepts to fully understand the rest of this post:
- You used your machine to build the application (using
Maven
, either previously installed or the wrapper). - You used your machine to run the application (either using the
spring-boot plugin
or thejar
file).
Step 4 - Dockerizing Spring Boot (or any executable .jar file)
Setup Docker
Before creating a container for the Django application and shipping it off, you need to install Docker on your local machine. For learning purpose, you will install Docker Community Edition. Select your OS from the list below and follow the setup instructions:
Make the docker App image
The next stage to improve your docker RoR workflow is adding a Dockerfile
to your project. The structure of a
Dockerfile
can be considered a series of instructions on how to build your container/image.
Start the Dockerfile by creating an empty file named Dockerfile
in the root of your project. Then, complete each
instruction according to the following example:
FROM openjdk:10-jre-slim
WORKDIR /app
COPY ./target/<your-file>-<version>/SNAPSHOT.jar /app
EXPOSE 8080
CMD ["java", "-jar", "<your-file>-<version>/SNAPSHOT.jar"]
The image is based on a slim Linux with JRE 10, on top of that we change the working directory and we copy the JAR file, then we just execute the same command as we did before when running from our machine. The EXPOSE instruction is telling Docker that the 8080 port can be exposed outside the container, but note that we’ll also need to make the port available when running it anyways.
Building and Running the Container
Building the docker container is very straight forward once you have Docker and Docker Machine on your system. The following
command will look for your Dockerfile
and download all the necessary layers required to get your container image
running. Afterwards, it will run the instructions in the Dockerfile
and leave you with a container that is ready to
start.
To build your Spring Boot docker container, you will use the docker build
command and provide a tag or a name for the container, so you can reference it later when you want to run it. The final part of the command tells Docker which directory to build from.
$ docker build -t spring-boot-tutorial .
The final step is to run the container you have just built using Docker:
$ docker run -it -p 8080:8080 spring-boot-tutorial
The command tells Docker to run the container and forward the exposed port 8080 to port 8080 on your local machine. After you run this command, you should be able to visit http://localhost:8080 in your browser.
$ docker ps -a
To turn off your Docker container, run:
$ docker stop container_id
Push to cloud
1. Create your app
In order to install your Spring Boot project, just create a new dockerize app via cli or admin panel and set a port to 8080.
2. Push your docker container
Then just build your image and upload it to the platform.
3. Set up resources
4. Logs and app status
5. Release your app
After to click on the release button, your spring boot docker tutorial will be deployed, Just click on the generated URL and you will get your app running.
Now you can deploy your Spring Boot app without a massive build time.
Bonus 1: SSL certificate for HTTPS
It's already done for you. If you need to connect your custom domain, SSL certificate will be provided for it.
Bonus 2: Autoscaling
With autoscaling the app will be scaled up when CPU and RAM load goes up and scaled down when it goes down.
Now you can deploy your Spring Boot app without a massive build time.