Step-by-step tutorial on how to build a spring boot docker
With Zaven, you will learn how to configure the Spring Boot development environment with Docker and docker-compose. In the first step, we will generate a project using the official Spring Initializr generator. It is an easy-to-use tool that will significantly speed up our work. We select the fields as follows to generate a project from Kotlin and Spring Boot version 2.0.6. in the “Group” field in the “Project metadata” section, enter the name of our project package. This is usually an inverted domain record identifying our project. I entered my blog domain there. Below, in the field labeled “Artifact”, we will enter the name of the artifact. Then in the “Dependencies” section on the right side we find and choose: JPA, PostgreSQL. We can configure our application using YAML files. You can find the configuration file in resources. The configuration file is named application.properties. Rename it to application.yml. The appropriate path to the configuration file is /appName/src/main/resources/application.yml We don’t supply any values directly because everything will be injected using a variable environment which we will put into a file called .env. These variables define the name of the application, active profile, port on which our application is available, a link to our database located in the docker container as well as login, password and name of our database.
How to configure docker-compose files and run the application?
If you don’t know what docker-compose is, I suggest reading the documentation. Our mini-infrastructure includes only a database service. Nevertheless, there is nothing to prevent you from adding other services, such as Mongodb. Now open the docker-compose.dev.yml file and save it in the main project directory. Thanks to docker-compose, we are able to clearly define our services, virtual network and provide variable environments. To run the application, select the .env file in the application startup settings and run the database container. To do this, enable the application startup configuration in the IDE, go to the EnvFile tab and check the Enable EnvFile box and add the file. At the final stage, perform the following steps: docker-compose -f docker-compose.dev.yml in BASH or PowerShell. Then create and run the application (shift + F10).