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.

Docker and Spring Boot tutorial – 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.

Docker and Spring Boot – 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.

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

Docker and Spring Boot tutorial – 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

Angular 7 tutorial: learn Angular 7 by example

Angular 7 tutorial: learn Angular 7 by example

Angular 7 - what are the new improvements and features? Keep on reading this Angular 7 tutorial and create your own web application with me to learn more about this modern web framework by example.

Read more
Android tutorial: starting another activity

Android tutorial: starting another activity

If you are an Android developer you may encounter a problem with app navigation during the app development process. In this Android tutorial I will describe a solution that makes code management much easier. Keep on reading and find out how to do it.

Read more
Effortless deployment of Azure CosmosDB JS functions with GitLab-CI

Effortless deployment of Azure CosmosDB JS functions with GitLab-CI

Working with CosmosDB JavaScript triggers and stored procedures can be problematic, especially when your application consists of three or four separate development environments. The solution we need is found in the effortless deployment of Azure CosmosDB JS functions with GitLab-CI.

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

Contact us

Estimate app cost!

back to top