[JAVA] Spring Boot starting with Docker

When I thought about developing a web application with Java Spring Boot, I decided to build a development environment using Docker while also studying Docker.

Operating environment

Spring Boot implementation

Download template

First, create a Spring Boot template with Spring Initializr.

  1. Select Gradle Project
  2. Change Group to hello (default is okay)
  3. Change ʻArtifact` to hello (default is okay)
  4. Add Web to Dependencies

スクリーンショット 2017-10-11 15.42.42.png

Then download the template with Generate Project.

Change build.gradle

Use Gradle to build your app and manage dependent libraries. The build script build.gradle is included in the template downloaded above, so change it as follows.

buildscript {
    ext {
        springBootVersion = '1.5.7.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'hello-world'
    version = '0.1.0'
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

If you use IntelliJ IDEA as the editor, it will install the necessary libraries without permission. If you are not using it, run gradlew clean build.

Changes to HelloApplication.java

Change src / main / java / hello / hello / HelloApplication.java in the downloaded template as follows.

package hello.hello;

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 HelloApplication {

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

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

}

Operation check

Let's check the operation here for the time being. The jar file should be created under build / libs. Execute the following command and check if it outputs Hello World from Docker.

$ ./gradlew build && java -jar build/libs/hello-world-0.1.0.jar
$ curl http://localhost:8080

Environment construction with Docker

Creating a Dockerfile

At this point, set up the environment with Docker. If Docker is not installed, download it from here and install it. Place Dockerfile in the root directory and describe the contents as follows.

FROM openjdk:jdk-alpine
VOLUME /tmp
RUN mkdir /app
WORKDIR /app
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar build/libs/hello-world-0.1.0.jar" ]

docker-compose settings

In the future, I think that there will be cases where I want to manage the container for applications and the container for DB separately, so I will use docker-compose. Place docker-compose.yml in the root directory and describe the contents as follows.

version: '2'
services:
    app:
        build: .
        image: tiqwab/boot:0.0.1
        ports:
            - "8080:8080"
        volumes:
            - .:/app

Build docker image & start container

Now that you're ready, build the docker image with the following command and start the container.

$ docker-compose up --build

In this state, try accessing http: // localhost: 8080 /. You should see Hello World from Docker.

From now on, you can start the container with the following command.

#Start-up
$ docker-compose up -d

#Stop
$ docker-compose stop

With the above, you can easily build a Spring Boot development environment using Docker.

reference

-Spring Boot with Docker development environment

Recommended Posts

Spring Boot starting with Docker
Spring Boot starting with copy
Spring Boot gradle build with Docker
Download with Spring Boot
Spring Boot environment construction with Docker (January 2021 version)
Create a Spring Boot development environment with docker
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Docker × Spring Boot environment construction
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
spring × docker
[Java] [Spring Boot] Specify runtime profile --Spring Boot starting with NetBeans
Database environment construction with Docker in Spring boot (IntellJ)
Create an app with Spring Boot 2
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Get validation results with Spring Boot
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Spring Boot starting from zero Part 2
Spring Boot starting from zero Part 1
Message cooperation started with Spring Boot
Processing at application startup with Spring Boot
Hello World with Eclipse + Spring Boot + Maven
Send regular notifications with LineNotify + Spring Boot
Perform transaction confirmation test with Spring Boot
Create Spring Boot-gradle-mysql development environment with Docker
Try using Spring Boot with VS Code
Start web application development with Spring Boot
Launch Nginx + Spring Boot application with docker-compose
I tried Lazy Initialization with Spring Boot 2.2.0
Spring Boot Form
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Asynchronous processing with Spring Boot using @Async
Image Spring Boot app using jib-maven-plugin and start it with Docker
Implement paging function with Spring Boot + Thymeleaf
Spring Boot Memorandum
gae + spring boot
Javaw.exe error when starting Spring Boot (STS)
(IntelliJ + gradle) Hello World with Spring Boot
Use cache with EhCashe 2.x with Spring Boot
Form class validation test with Spring Boot
Run WEB application with Spring Boot + Thymeleaf