DEV Community

Cover image for Spring Boot Development Environment
Lohrran
Lohrran

Posted on

Spring Boot Development Environment

Introduction

For health development, the best practice is to have a homogeneous setting across the different environments, in what the best way to do it if not using Docker.

Here below I'll explain how to configure an environment for development using Spring Boot and Mongo Database, however, you can use another database without problem.

We need do three main things to be able to develop using docker, the steps are:

  1. Create a Project using the standard means to do it.
  2. Configurate pom.xml to package .jar files.
  3. Create Docker files (Dockerfile, and docker-compose.yml).
  4. Configurate the Debugger in the IDE, here we will use Intellij.

Create Project

When creating the Spring Boot project using the web site, we will configure it like below:

Image description

You can change some configurations if needed, the only important things here are:

  • Project: Maven
  • Language: Java
  • Packaging: Jar

Make sure that your pom.xml has the path to the main class and the option repackages set, it will be necessary to generate the .jar file.

<build>  
   <plugins>  
      <plugin>  
         <groupId>org.springframework.boot</groupId>  
         <artifactId>spring-boot-maven-plugin</artifactId>  
         <executions>  
            <execution>  
               <configuration>  
                  <mainClass>com.trantorinc.spring-boot-local-dev-docker.Application</mainClass>  
               </configuration>  
            </execution>  
            <execution>  
               <goals>  
                  <goal>repackage</goal>  
               </goals>  
            </execution>  
         </executions>  
        </plugin>  
   </plugins>  
</build>
Enter fullscreen mode Exit fullscreen mode

Configure Environment

We will need two types of docker files, Dockerfile and docker-compose.yml, for setting up the development environment:

Dockerfile

FROM maven:3.8.4-jdk-11  

ENV JAVA_TOOL_OPTIONS -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005  
ENV JAVA_DEBUG true
Enter fullscreen mode Exit fullscreen mode

Above you can see that we are using the official image of Maven and have two variables, that are necessary for configuring the debugger, what it basically does is expose java debug options into the 5005 port.

docker-compose

  1. Spring Boot
version: '3.8'  
services:  
  my-app:  
    container_name: my-spring-boot-app
    build:  
      context: .  
      dockerfile: Dockerfile  
    command: java -jar target/myapplicationname-0.0.1-SNAPSHOT.jar  
    ports:  
      - 8080:8080  
      - 5005:5005  
    volumes:  
      - .:/usr/src/mymaven:rw  
    working_dir: /usr/src/mymaven  
Enter fullscreen mode Exit fullscreen mode

What is being done above? First, we build the docker image and then execute a command to execute the .jar that was generated. It also needs to expose two ports to this work perfectly, the port 8080 is for accessing the localhost Restful API, and the port 5005 is for accessing the debug option. Besides that, we configure a volume and working directly for accept changes from our local project.

  1. Mongo
  mongodb:  
    image: mongo  
    container_name: mongodb  
    ports:  
      - 27017:27017  
    volumes:  
      - /data
    environment:  
      - MONGO_INITDB_ROOT_USERNAME=rootuser 
      - MONGO_INITDB_ROOT_PASSWORD=rootpass
      - MONGO_INITDB_DATABASE=mydatabasename
volumes:  
  data: { }  

networks:  
  default:  
    name: mongodb_network
Enter fullscreen mode Exit fullscreen mode

Above we simply create a service to run the mongo image and configure the parameters of the database. After it, we create a network to connect the two containers created.
We also can create a service for mongo express if needed:

  mongo-express:  
    image: mongo-express:0.54  
    container_name: mongo-express  
    restart: always  
    ports:  
      - 8081:8081  
    environment:  
      - ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser  
      - ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
      - ME_CONFIG_MONGODB_SERVER=mongodb
Enter fullscreen mode Exit fullscreen mode

After finishing this part we need one more configuration, in src/main/resources we open the file application.properties and paste the following information:

spring.data.mongodb.authentication-database=admin  
spring.data.mongodb.username=rootuser  
spring.data.mongodb.password=rootpass  
spring.data.mongodb.database=mydatabasename  
spring.data.mongodb.port=27017  
spring.data.mongodb.host=mongodb  
spring.data.mongodb.auto-index-creation=true
Enter fullscreen mode Exit fullscreen mode

In case you need to initialize values you can execute commands in mongo when starting up the environment. It just needs to make some adjustments.

  • Add the following line in the volumes of mongodb service:
    • ./devops/dev/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:r.
  • Inside the .js file should have something like this:
db = new Mongo().getDB('mydatabase'); 
db.createCollection('users'); 
db.users.insertMany([ 
    { 
        username: 'user01', 
        password: '123456', 
        email: 'user01@email.com' 
    }, 
    { 
        username: 'user02', 
        password: '654321', 
        email: 'user02@email.com' }, 
    { 
        username: 'user03', 
        password: '456456', 
        email: 'user03@email.com' 
    } 
]);
Enter fullscreen mode Exit fullscreen mode

Configure Debug

Now to connect it to your IDE and be possible to debug set it, here we will use Intellij. So what you need to do is create one Remote JVM Debug.

  1. Click on Edit Configuration:

Image description

  1. Create a Remote JVM Debug and configure below:

Image description

Execution

  1. Execute follow command to run environment
docker-compose up -d
Enter fullscreen mode Exit fullscreen mode
  1. Click the symbol debug button:

Image description

Conclusion

After a long day configuring it, I thought that a manual would be of good use for the community because I face several problems trying to configure it until arrive at this solution. Any questions I will be glad to try to answer them.
Access te files here!

Enjoy!

Top comments (0)