Next, I’ll talk about my experience of launching the docked Spring Boot application on free virtual machines of the
Heroku cloud service. One of the main advantages of this provider is that it allows you to create free virtual machines with limited hours of operation, and for this you only need to register. Even the payment details are not required to confirm, although if you confirm them you can get additional bonuses. More information about their price can be found
here . From my point of view, their policy in terms of free resources is almost unique.
And so, after you have created an application on Heroku, there are
several ways to deploy your code in it
- commit to heroku git repository
- binding the application to the repository on github
- use docker container
Next, we will consider the last of these methods. To continue, we will need the following applications.
The last of these tools will give us the opportunity to perform all actions to work with the cloud from the command line.
')
We start by creating a Spring Boot application through the
Spring Initializr . Add Spring Web Starter as dependencies. It should be enough.
In the same package in which the main class of the application is located, we add the simplest class of the REST controller so that the application somehow shows signs of life.
@RestController public class DemoController { @GetMapping("/check") public String check() { return "Application is alive"; } }
Add to the file
application.properties server.port=${PORT:8080}
This setting is extremely important for running the container on Heroku. The fact is that in the internal network of the service, the application is launched on some port that is free at the time of launch, the number of which is transmitted through the environment variable PORT. In addition, the application must have time to connect to this port within the first
60 seconds after launch, otherwise it will be stopped.
In the
plugins section of the
pom.xml file
we add the
dockerfile-plugin from Spotify, which will help us with the assembly of the docker image of our application.
<plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.4.6</version> <executions> <execution> <id>default</id> <goals> <goal>build</goal> <goal>push</goal> </goals> </execution> </executions> <configuration> <repository>registry.heroku.com/${project.artifactId}/web</repository> <tag>latest</tag> <buildArgs> <JAR_FILE>${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin>
This configuration of the plugin will run the image build as part of the install target, and the push image into the Heroku docker repository, as part of the deploy target. By the way, to be able to run maven deploy, you need to disable the deploy of the assembled jar file (we do not need to upload it anywhere). This can be done using the
maven.deploy.skip option in the properties section of the pom.xml file.
<properties> <java.version>1.8</java.version> <maven.deploy.skip>true</maven.deploy.skip> </properties>
Next, create a
Dockerfile in the root folder of the project (next to pom.xml)
FROM openjdk:8-jdk-alpine ARG JAR_FILE RUN mkdir -p /apps COPY ./target/${JAR_FILE} /apps/app.jar COPY ./entrypoint.sh /apps/entrypoint.sh RUN chmod +x /apps/entrypoint.sh ENTRYPOINT ["/apps/entrypoint.sh"]
As you can see, we pass here the name and the assembled jar file as an assembly argument. The value of this argument is set in the maven plugin settings.
The
entrypoint.sh script will also be placed at the root of the project and it will be very simple.
Pay attention to the JVM parameters limiting memory and especially the first two parameters. They include a special memory management mode, which is necessary when the java application is launched inside the Docker container. The memory available to the application on free Heroku machines is limited to 512MB. Exceeding this limit will terminate the application. If you want to use Java 9+, then the first two options need to be replaced with one
-XX: + UseContainerSupport . You can read more
here .
After completing these steps, try running
mvnw clean install
If everything is done correctly, then the application should be assembled and a docker image for it. Check if the image was created with the command
docker images
Now let's start setting up the cloud and use the already mentioned Heroku CLI for this. Of course, to perform these actions you will need to register an account on Heroku.
First of all, we need to log in. To do this, run the command
heroku login
and follow the instructions.
After that, you need to log in to the heroku Docker repository. To do this, run the command
heroku container:login
without it, we will not be able to push our docker image.
Next, create an application using the command
heroku apps:create <app-name>
Please note that the name of the application must match the name of the artifact, which is indicated in
pom.xml . Perhaps here you will have to spend some time searching for the name of the application that has not yet been set.
After the application is created, run
mvnw clean deploy
and we wait while the application will be collected and there will be a push docker-image. Please note that push is only possible if the image name matches the
registry.heroku.com/<app-name>/web template and an application <app-name> has been created. If you look at the maven plug-in settings, you will see that everything has been done that way.
The final step is for the image to be deployed and running this command.
heroku container:release web --app=<app-name>
After that, follow the link
https: // <app-name> .herokuapp.com / check and after a while you will see the text that will be displayed by the controller handler.
Another way to open a running application in a browser is with the command
heroku open --app=<app-name>
In case something does not work, logs can be viewed in the Heroku Web interface or by the command
heroku logs
That's all! Hope this guide was helpful to you!
Some useful links
1. Heroku CLI
documentation devcenter.heroku.com/categories/command-line2. About the features of memory management in billed Java
devcenter.heroku.com/articles/java-memory-issues3.
devcenter.heroku.com/categories/deploying-with-docker4. An example Java application from Heroku. There is another way to deploy, but it is useful to look at
github.com/heroku/java-getting-started5. Heroku tutorial on launching a Java application (not via Docker)
devcenter.heroku.com/articles/getting-started-with-java?singlepage=true