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.
First, create a Spring Boot template with Spring Initializr.
Gradle Project
Group
to hello (default is okay)Dependencies
Then download the template with Generate Project
.
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
.
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);
}
}
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
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" ]
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
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.
-Spring Boot with Docker development environment
Recommended Posts