Build a Spring Boot environment using Docker. To check the operation, display Hello World on the browser.
--macOS Catalina version 10.15.5
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
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.1 Create a Gradle project template with spring initializr

2.2 Download Gradle project template

2.3 Expand to project
Create a server directory directly under the project directory.
Extract ʻapi.zipdownloaded in 2.2 and move the contents directly underserver`.
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);
    }
}
//docker environment build
% docker-compose build
//Start docker in the background
% docker-compose up -d
//Inspection in java container
% docker-compose exec java bash
//gradle build
root@62acca270468:/srv# sh gradlew build
...
BUILD SUCCESSFUL in 1m 38s
5 actionable tasks: 3 executed, 2 up-to-date
Make sure that the build directory is created directly under the server.
root@62acca270468:/srv# java -jar build/libs/api-0.0.1-SNAPSHOT.jar
Let's access http: // localhost: 8080 /.
If Hello World is displayed, it is successful.
-Docker + Spring-boot start -Spring Boot starting with Docker
Recommended Posts