Docker and Spring Boot tutorial

published in Tutorials, Web Development
by Slawomir Onyszko

Do you need to build a simple Spring Boot app with Docker? Keep on reading this Docker and Spring Boot tutorial and find out how to configure the Spring Boot development environment using Docker and docker-compose.

Project creation

In the first step, we will generate a project using the official generator Spring Initializr. This is a simple-to-use tool that will greatly speed up our work.

We select the fields as follows to generate the project with Kotlin and Spring Boot version 2.0.6. in the field “Group” from the section “Project Metadata”, we enter the name of our project package. Usually this is an inverted record of the domain identifying our project. I have entered the domain of my blog there. Below, in the field marked Artifact”, we will enter the name of the artifact. Next, in the section named “Dependencies” on the right, we find and choose: JPA, PostgreSQL; just as in the picture below.

Docker and Spring Boot tutorialIf you need more information, I recommend that you read Spring Initializr Reference Guide.

Spring Boot Docker – application configuration

We can configure our application with the help of YAML files. You can find the configuration file in resources. The configuration file is named application.properties. Change its name to application.yml.

The relevant path to the configuration file is /appName/src/main/resources/application.yml

The configuration file appears as follows:

server:
  port: ${SERVER_PORT}

spring:
  application:
    name: ${APPLICATION_NAME}
  profiles.active: ${ACTIVE_PROFILE}
  jpa:
    properties:
      hibernate:
        temp:
          use_jdbc_metadata_defaults: false
        ddl-auto: update
        hbm2ddl.auto: create-drop
        naming-strategy: org.hibernate.cfg.EJB3NamingStrategy
        show-sql: true
    database-platform: org.hibernate.dialect.PostgreSQLDialect
  jackson:
    property-naming-strategy: CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES
    serialization:
      indent_output: true
  datasource:
    url: ${DATABASE_URL}
    username: ${POSTGRES_USER}
    password: ${POSTGRES_PASSWORD}
    driver-class-name: org.postgresql.Driver

We do not directly provide any values because everything will be injected using the variable environment that we will enter in the file entitled .env.

 APPLICATION_NAME=tasks-rest-api
ACTIVE_PROFILE=development
SERVER_PORT=3000
DATABASE_URL=jdbc:postgresql://localhost:5432/tasks-rest-api-db
POSTGRES_USER=tasks-rest-api-admin
POSTGRES_PASSWORD=password
POSTGRES_DB=tasks-rest-api-db 

These variables define the name of the application, the active profile, the port on which our application is available, the connection link to our database located in the docker container as well as the login, password and name of our database.

File configuration docker-compose

If you don’t know what docker-compose is, I suggest you read documentation.

Only database service can be found in our mini-infrastructure. Nevertheless, nothing prevents the addition of other services e.g. mongodb.

Now it is necessary to open docker-compose.dev.yml and save it in the main project catalog.

 version: '2'
services:

  tasks-rest-api-db:                      
    image: postgres:10                    
    container_name: 'tasks-rest-api-db'    
    ports:
      - '5432:5432'                        
    env_file:
      - .env    

Thanks to docker-compose we are able to clearly define our services, virtual network and provide variable environments.

Docker and Spring Boot – starting the application

To run the application, you must indicate the .env file in the application launch settings and launch the database container.
To do this, enable the application startup configuration in the IDE, go to the EnvFile tab and check the Enable EnvFile option and add the file.

spring boot docker

Now execute the following: docker-compose -f docker-compose.dev.yml upin BASH or PowerShell. Then make and run the application (shift + F10).

Summary

In this Docker and Spring Boot tutorial, I have shown you how to set up the Spring Boot development environment using Docker and docker-compose. Did you manage to go through all the steps? Let me know in the comments below. Please have a look at the other tutorials on our blog.

Slawomir Onyszko Mobile Developer

Sławek is our mobile developer who mostly takes care of creating Android applications. He is constantly enhancing his skills of advanced Android app development and he wants to share this knowledge via Zaven’s blog. In his spare time Sławek enjoys watching a good movie at the cinema.

Popular posts

Artificial Intelligence in Medicine

Artificial Intelligence in Medicine

The active development of Artificial Intelligence (AI) plays an increasingly important role in the analysis, diagnosis and monitoring of patient treatment. It also improves patient-doctor contact and automatic reporting. Will modern technologies revolutionize the current health care system? This is what you will learn from the article. What is Artificial Intelligence in medicine? In a […]

Read more
IoMT – Internet of Medical Things

IoMT – Internet of Medical Things

The revolution brought by the Internet of Things (IoT) is beginning to take over more and more areas of everyday life. This also includes the use of such solutions in medicine and health care. This phenomenon is already so common that it has its name – IoMT (Internet of Medical Things). How does IoMT support […]

Read more
Mobile Healthcare Applications

Mobile Healthcare Applications

The development of mobile applications dedicated to healthcare has brought about a significant change in the once traditional healthcare industry. What used to involve spending a lot of money, waiting in long queues or consulting many professionals is now often reduced to using a mobile application. By using it, we will make an appointment, consult […]

Read more
Mobile Apps

Get your mobile app in 3 easy steps!

1

Spec out

with the help of our
business analyst

2

Develop

design, implement
and test, repeat!

3

Publish

get your app out
to the stores


back to top