Recently, I haven't had the opportunity to build the source from scratch for maintenance and document creation, and I tend to forget about the basics.
At that time, I happened to watch this video.
How to create Docker Image and run Java App (Spring Boot Jar) in a Docker Engine | Tech Primers(2017) https://www.youtube.com/watch?v=FlSup_eelYE
The content of the video is very easy to understand, and by the way, I remembered that I had never made it from scratch, so I carried out construction work for studying.
Make the Springboot app run on the Docker container. Make it accessible from the outside with the Rest API.
Allow Docker to run in a windows environment.
Can be downloaded from this official https://hub.docker.com/editions/community/docker-ce-desktop-windows Start [Docker Desktop Installer.exe] and follow the procedure
Start by rebooting after the installer
Add proxies and below to the ~ / .docker / config.json file.
{
"credsStore": "desktop",
"stackOrchestrator": "swarm",
"credStore": "desktop",
"proxies":
{
"default":
{
"httpProxy": "http://127.0.0.1:3001",
"httpsProxy": "http://127.0.0.1:3001",
"noProxy": "*.test.example.com,.example2.com"
}
}
}
○ Create a Spring boot Project https://start.spring.io/
Added Rest Repository to Dependency to create RestAPI.
After making the settings, press the GENERATE button to download the ZIP file.
Create a controller package under poc.dockerspringboot and create a sample file
package poc.dockerspringboot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/rest/message/helloWorldOnDocker")
public class HelloWorldOnDocker {
@GetMapping
public String hello(){
return "Hello from Docker Container";
}
}
Perform mvn clean install
Create a Dockerfile directly under the project
FROM adoptopenjdk:11-jre-openj9
ADD target/docker-springboot.jar docker-springboot.jar
EXPOSE 8081
ENTRYPOINT ["java", "-jar", "docker-springboot.jar"]
In OpenJDK, use adoptopenjdk that can be used for commercial purposes. https://hub.docker.com/_/adoptopenjdk
Add the generated module to Docker. A module is created under target, but since the file name of jar is long, specify the output name with [finalName].
<build>
<finalName>docker-springboot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Reference: Spring-boot-maven-plugin setting information https://spring.pleiades.io/spring-boot/docs/current/maven-plugin/reference/html/
Specify the port you want to open in the Docker container
Inside the container, the default port when spring-boot starts is 8080 If you want to change this value, add server.port = 8082 etc. to application.properties
When Docker is started, the port that is started internally and the port that is open to the outside are linked.
Start the module with the jar command. By executing this command, the embedded server (Tomcat) will start and accept access.
#Image creation
docker build -f Dockerfile -t docker-spring-boot .
#Confirmation of creation
docker images
#Container startup
docker run -p 8081:8080 docker-spring-boot
http://localhost:8081/rest/message/helloWorldOnDocker
There are no new technical discoveries, but occasionally doing this kind of work clears your thinking. Described as a guideline so that it can be transferred later.
Recommended Posts