Spring Boot + Docker Java development environment construction

Overview

Build a Java development environment with Spring Boot with Docker. Use Java 11 and gradle.

Up to the point where Hello World! Is displayed. (Update: Added remote debugging method)

Finished product

https://github.com/tomoyuki-mikami/spring-boot-docker

Execution method

$ cd docker
$ docker-compose up -d

environment

MacOS Catalina 10.15.2 Docker for Mac 2.1.0.5 IntelliJ IDEA Ultimate 2019.3.1 OpenJDK 11 (installed with homebrew)

Local procedure

Creating a project

Without IntelliJ

https://start.spring.io/ If you make the same selection here and Generate, it will be downloaded as a zip, so use that.

If you have IntelliJ
  1. Create New Project in IntelliJ スクリーンショット 2019-12-29 10.44.27.png
  2. Java 11 specification, Spring Initializer is by default スクリーンショット 2019-12-29 10.45.33.png
  3. Select Gradle Config, Java, Jar, 11. Other than that スクリーンショット 2019-12-29 10.45.57.png
  4. Dependencies selected the following, but it is unnecessary if you just want to display Hello World Later, I put in various things to connect to MySQL, create a login function, and create a View. Rather, just displaying Hello World first does not start, so I commented it out in build.gradle. スクリーンショット 2019-12-29 10.46.42.png
  5. Specify the project name and the location to create the project スクリーンショット 2019-12-29 10.47.05.png
    Now when you press FINISH, the project will be created automatically and gradle will be run the first time. If you are using the latest IntelliJ, the directory under src is not created automatically, so create it manually.

Creating Hello World

Modify Application.java under src / main / java / com.mika.app

Application.java


package com.mika.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class Application {

    @RequestMapping("/")
    String Index() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Try running it in IntelliJ at this stage → Maybe it will fail. Press the run button next to public class Application {. スクリーンショット 2019-12-29 11.07.05.png

I need to comment out because I put too many things in dependencies and it works in vain.

Comment out part of build.gradle

build.gradle


dependencies {
//    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
//    implementation 'org.springframework.boot:spring-boot-starter-security'
//    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
//    runtimeOnly 'mysql:mysql-connector-java'
//    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
    testImplementation 'org.springframework.security:spring-security-test'
}

Now run again Display http: // localhost: 8080 and confirm that Hello World is displayed. After confirming, stop with the stop button.

Create Dockerfile, docker-compose.yml

Create a docker directory and create Dockerfile and docker-compose.yml under it The reason for using docker-compose is that we plan to build MySQL and nginx here in the future.

Dockerfile


FROM openjdk:11

ENV APP_ROOT /app

COPY . $APP_ROOT
WORKDIR $APP_ROOT

ENTRYPOINT ["sh", "./gradlew", "bootRun"]

docker-compose.yml


version: '3'

services:
  web:
    build:
      #Specify the path required for copying the app in the Dockerfile
      context: ../
      #The location of the Dockerfile as seen from the context path
      dockerfile: docker/Dockerfile
    volumes:
      # :The left part fits your environment
      - ~/Dev/java/spring-boot:/app
    ports:
      - "8080:8080"

Start Docker environment

$ cd docker
$ docker-compose up

Confirm that it is started, and confirm that http: // localhost: 8080 is displayed again.

HotDeploy Since we are using spring-boot-devtools, HotDeploy is applied. If you change the characters of Hello World in Application.java You can see Java restart after a few seconds (from the docker-compose up log) If you reload the browser, you can see that the characters have changed.

Remote debugging with IntelliJ

IntelliJ settings

Since I want to set a breakpoint in development and stop it, I set remote debugging. First, open Run → Edit Configurations ... from the IntelliJ menu. Select Remote from the + button in the upper left. With the following settings, press OK to close.

Add the following to build.gradle

build.gradle


bootRun {
    // for remote debug
    jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"
}
Fix docker-compose.yml

Since port 5005 is used for remote debugging, it is described.

docker-compose.yml


ports:
   - "8080:8080"
   # for remote debug
   - "5005:5005"
start docker

Do docker-comose up again and check that it is in the standby state.

web_1  | > Task :bootRun
web_1  | Listening for transport dt_socket at address: 5005

Put a breakpoint in Hello World in Application.java If you access http: // localhost: 8080, you can see that it stops at that location. スクリーンショット 2019-12-29 17.28.56.png

in conclusion

For the time being, I have summarized how to build it easily.

Recommended Posts

Spring Boot + Docker Java development environment construction
Docker × Spring Boot environment construction
java development environment construction
Spring Boot environment construction with Docker (January 2021 version)
[Spring Boot] Environment construction (macOS)
Create a Spring Boot development environment with docker
Database environment construction with Docker in Spring boot (IntellJ)
Java + Spring development environment construction with VirtualBox + Ubuntu (Xfce4)
◆ Spring Boot + gradle environment construction memo
[Java] Environment construction
Java environment construction
Java development environment
[Spring] Environment construction
Docker environment construction
Create Spring Boot development environment on Vagrant
[Processing x Java] Construction of development environment
Spring Boot environment construction memo on mac
Laravel development environment construction with Docker (Mac)
Create Spring Boot-gradle-mysql development environment with Docker
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Try Spring Boot 1 (Environment construction ~ Tomcat startup)
Java development environment memo
Rails Docker environment construction
Spring boot development-development environment-
JAVA + STS (Spring Tool Suite) environment construction procedure
A reminder of Docker and development environment construction
Build a development environment for Docker, java, vscode
LINE Bot x Java (Spring Boot) construction procedure
Wordpress local environment construction & development procedure with Docker
Introduction to Java development environment & Spring Boot application created with VS Code
Rails6 development environment construction [Mac]
WSL2 + VSCode + Docker development environment
Java development environment construction on Mac-JDK Install (2020 preservation version)
BEAR application Docker development environment construction example (docker-sync, Mutagen)
[Mac] VS Code development environment construction (Java, Gradle, Node.js)
MySQL 5.7 (Docker) environment construction memo
Java development environment (Mac, Eclipse)
JavaFX environment construction in Java 13
Redmine (Docker) environment construction memo
CICS-Run Java application-(4) Spring Boot application
[Docker] Rails 5.2 environment construction with docker
Spring Boot starting with Docker
AtCoder Challenge Environment Construction (Java 8)
Build Java development environment with WSL2 Docker VS Code
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
[Environment construction] Build a Java development environment with VS Code!
Try to build a Java development environment using Docker
[Docker] postgres, pgadmin4 environment construction
React environment construction with Docker
Docker × Java Building a development environment that is too simple
Web application development environment construction in Java (for inexperienced people)
Java web application development environment construction with VS Code (struts2)
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Introducing Spring Boot2, a Java framework for web development (for beginners)
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~
Create a Java and JavaScript team development environment (gradle environment construction)
Build WebAPP development environment with Java + Spring with Visual Studio Code
[Java development environment construction] Install OpenJDK 11 (Java 11) on macOS with Homebrew.
Spring Boot + Java + GitHub authentication login
Node.js environment construction with Docker Compose
Spring Boot application development in Eclipse