This is inspired from Spring Boot Tutorial (Spring Boot with Docker).
From above tutorial, it explains how to
Change from java to kotlin and jdk version using Docker technique.
Let’s start
Environment
- OS Mac OS Big Sur
- M1 Chip Mac
- JDK 11
- Docker Desktop
Steps
- Create project from Spring Starter
- Implement Controller
- Test with Debug localhost:8080/hello
- Create build with gradle task
- Copy jar file under workspace
- Create Dockerfile
- Build Docker image
- Create container with docker run
- Access this from local browser
Create project from Spring Starter
Sprint Starter (this) is powerful tool to create Spring Boot project.
Set up from GUI and create Spring Boot project.
This is the setting for the project

Select gradle, Kotlin, and add “Spring Web” <- this is to implement Rest API
Implement Controller
package com.daiji110.demo.controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class HelloController {
@GetMapping("/hello")
fun helloRequest() : String {
return "hello";
}
}
This is simple Controller can access by /hello
Return string “hello”.
Test with Debug localhost:8080/hello
Now, code is ready, Let’s run it.
I use IntelliJ to open above project, just “Run” -> “Debug”
After Spring Boot project is running, you can access localhost:8080/hello from browser.
And just show “hello” on the screen.
Create build with gradle task
There are some ways to build above project.
The easiest way is to run gradle command from IDE.

IntelliJ has gradle task tab at the right.
“build” -> “build” is the target to build jar file.
After this task, we can see jar file under /build/libs/demo-0.01-SNAPSHOT.jar
This is Spring Boot application jar.
Copy jar file under workspace
Next is prepare workspace for Docker.
Anywhere is fine. I created blank directory. named workspace
Copy above jar file under this workspace
Create Dockerfile
Next is Dockerfile
Make Dockerfile under workspace directly
The contents is
FROM --platform=linux/x86_64 openjdk:18-ea-11-jdk-alpine
VOLUME /tmp
ARG JAR_FILE=target/*.jar
COPY demo-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /app.jar ${0} ${@}"]
* –platform=linux/x86_64 is needed for M1 chip
This is from here. Thanks
Use openjdk:18-ea-11-jdk-alpine. JDK 11, and Alpine. Alpine is lightweight linux.
Copy local file to Docker space, and run java -jar commands using ENTRYPOINT.
Build Docker image
Let’s build Docker image from command.
docker build -t springapp .
Go to workspace and type above command.
Docker image name is “springapp”
Now, Docker image is ready.
Create container with docker run
This is the last thing to do (without test)
Let’s start container from image.
docker run -p 9000:9000 springapp --server.port=9000
Use 9000 as port local and docker
Now, start spring boot

Can see Spring boot text on the shell and no error, start embed tomcat with 9000 port.
Access this from local browser
Let’s check above spring boot app result from local browser.
Now, can access localhost:9000/hello
You can see

コメント