Docker and Spring Boot tutorial

Tutorials, Web Development

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 tutorial
If 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 up in 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

From Hype to Hard Hats: Practical Use Cases for AI chatbots in Construction and Proptech.

From Hype to Hard Hats: Practical Use Cases for AI chatbots in Construction and Proptech.

Remember the multimedia craze in the early 2000s? It was everywhere, but did it truly revolutionize our lives? Probably not. Today, it feels like every piece of software is labeled "AI-powered." It's easy to dismiss AI chatbots in construction as just another tech fad.

Read more
Fears surrounding external support. How to address concerns about outsourcing software development?

Fears surrounding external support. How to address concerns about outsourcing software development?

Whether you’ve had bad experiences in the past or no experience at all, there will always be fears underlying your decision to outsource software development.

Read more
What do you actually seek from external support? Identify what’s preventing you from completing a project on time and within budget

What do you actually seek from external support? Identify what’s preventing you from completing a project on time and within budget

Let’s make it clear: if the capabilities are there, a project is best delivered internally. Sometimes, however, we are missing certain capabilities that are required to deliver said project in a realistic timeline. These may be related to skills (e.g. technical expertise, domain experience), budget (hiring locally is too expensive) or just capacity (not enough manpower). What are good reasons for outsourcing software development?

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