[Java] Docker × Spring Boot environment construction

2 minute read

Docker × Spring Boot environment construction

Overview

Build a Spring Boot environment using Docker. To check the operation, display Hello World on the browser.

Environment

  • macOS Catalina version 10.15.5
  • Docker version 19.03.8
  • docker-compose version 1.25.5
  • openjdk 14.0.1 2020-04-14
  • Spring Boot 2.3.1

Constitution

The final configuration will be as follows.

├── docker-compose.yml
└── server
    ├── HELP.md
    ├── build
    │  ├── classes
    │  │  └── java
    │  │  ├── main
    │   │   │  └── com
    │ │ │  │  └── example
    │   │   │  └── api
    │   │   │  └── ApiApplication.class
    │   │  └── test
    │  │  └── com
    │  │  └── example
    │   │  └── api
    │  │  └── ApiApplicationTests.class
    │  ├── generated
    │ │ │  └── sources
    │  │  ├── annotationProcessor
    │ │ │   │  └── java
    │   │   │  ├── main
    │   │   │  └── test
    │  │  └── headers
    │  │  └── java
    │  │  ├── main
    │   │  └── test
    │  ├── libs
    │  │  └── api-0.0.1-SNAPSHOT.jar
    │  ├── reports
    │  │  └── tests
    │   │  └── test
    │  │  ├── classes
    │   │   │  └── com.example.api.ApiApplicationTests.html
    │  │  ├── css
    │ │ │  │ ├── base-style.css
    │   │   │  └── style.css
    │   │  ├── index.html
    │  │ ├── js
    │   │   │  └── report.js
    │ │ │ └── packages
    │  │  └── com.example.api.html
    │  ├── resources
    │  │  └── main
    │   │  ├── application.properties
    │   │  ├── static
    │  │  └── templates
    │  ├── test-results
    │   │  └── test
    │   │  ├── TEST-com.example.api.ApiApplicationTests.xml
    │   │  └── binary
    │   │  ├── output.bin
    │  │  ├── output.bin.idx
    │   │  └── results.bin
    │  └── tmp
    │  ├── bootJar
    │  │  └── MANIFEST.MF
    │  ├── compileJava
    │  └── compileTestJava
    ├── build.gradle
    ├── gradle
    │  └── wrapper
    │  ├── gradle-wrapper.jar
    │  └── gradle-wrapper.properties
    ├── gradlew
    ├── gradlew.bat
    ├── settings.gradle
    └── src
        ├── main
        │  ├── java
        │  │  └── com
        │  │  └── example
        │   │  └── api
        │   │  └── ApiApplication.java
        │  └── resources
        │  ├── application.properties
        │  ├── static
        │  └── templates
        └── test
            └── java
                └── com
                    └── example
                        └── api
                            └── ApiApplicationTests.java

Procedure

1. Create docker-compose.yml

This time, it is a simple configuration with only one java container.

docker-compose.yml


version: '3.6'
services:
  java:
    image: openjdk:14-slim
    ports:
      -8080:8080
    tty: true
    volumes:
      -./server:/srv:cached
    working_dir: /srv

2. Gradle project creation

2.1 Create a Gradle project template with spring initializr Screenshot 2020-07-10 11.37.29.png

2.2 Download the Gradle project template ![Screenshot 2020-07-10 11.43.22.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/290840/dda9d024-8702-5110-ec6d-(a5ee184fb4f2.png)

2.3 Deploy to project Create a server directory directly under the project directory. Extract the api.zip downloaded in 2.2 and move the contents directly under server.

3. Edit the application file

Edit server/src/main/java/com/example/api/ApiApplication.java as follows.

server/src/main/java/com/example/api/ApiApplication.java


package com.example.api;

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;

@SpringBootApplication
@RestController
public class ApiApplication {

    @RequestMapping("/")
    public String home() {
        return "Hello World";
    }

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

}

4. Start Docker

// build the docker environment
% docker-compose build

// start docker in the background
% docker-compose up -d

5. Gradle build

// inspection in java container
% docker-compose exec java bash

// gradle build
[email protected]:/srv# sh gradlew build
...
BUILD SUCCESSFUL in 1m 38s
5 actionable tasks: 3 executed, 2 up-to-date

Make sure that a build directory is created directly under server.

6. Run the application

[email protected]:/srv# java -jar build/libs/api-0.0.1-SNAPSHOT.jar

7. Operation check

Let’s access http://localhost:8080/. If Hello World is displayed, you are successful.

Reference