Skip to content

Commit 9e26741

Browse files
authored
Merge pull request #4 from NileshGule/main
Added Java Spring Boot example to docker init demos
2 parents a6553bf + 1964bd5 commit 9e26741

File tree

11 files changed

+821
-0
lines changed

11 files changed

+821
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This repository includes sample applications in the following programming langua
2222
- [Rust](./rust)
2323
- [Go](./go)
2424
- [ASP.NET](./dotnet)
25+
- [Java](./java)
2526

2627
## Prerequisites
2728

@@ -69,6 +70,7 @@ Here's a list of the sample applications available in each language folder:
6970
- [Python: A basic "Hello, Docker!" Web application](./python)
7071
- [Rust: A command-line application that prints "Hello, Docker!" to the console](./rust)
7172
- [Go: A minimal web server that responds with "Hello, Docker!" to HTTP requests](./go)
73+
- [Java: A simple "Hello Docker!" Spring Boot Rest Service](./java)
7274

7375

7476
Feel free to explore each folder and modify the sample apps to experiment with Docker and your preferred language.

java/.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/

java/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# A Simple Spring Boot Rest Service
2+
3+
Example used to demonstrate `docker init` CLI for a simple Spring Boot Rest Service
4+
5+
This project contains a web service that will accept HTTP GET requests at
6+
`http://localhost:8080/greeting`.
7+
8+
9+
## Run the application
10+
11+
First, clone the repository
12+
13+
```
14+
git clone https://github.com/dockersamples/docker-init-demos
15+
cd docker-init-demos/java/
16+
```
17+
Build the application by running the command `mvn clean install`
18+
You can simply use the following `mvn spring-boot:run` to test the application.
19+
20+
In the browser, navigate to `` to see the greeting.
21+
22+
## Accessing the application
23+
24+
```
25+
> curl https://http://localhost:8080/greeting
26+
27+
{"id":1,"content":"Hello, Docker!"}
28+
29+
```
30+
31+
## Using Docker init
32+
33+
Run the following command:
34+
35+
```
36+
docker init
37+
This utility will walk you through creating the following files with sensible defaults for your project:
38+
- .dockerignore
39+
- Dockerfile
40+
- docker-compose.yaml
41+
42+
? What application platform does your project use? Java
43+
? What's the relative directory (with a leading .) for your app? ./src
44+
? What version of Java do you want to use? 17
45+
? What port does your server listen on? 8080
46+
```
47+
48+
## Accessing the WebApp
49+
50+
```
51+
docker compose up -d
52+
```
53+
54+
Note: Sometime the docker compose up command will fail with the following error:
55+
56+
```
57+
Error: Could not find or load main class org.springframework.boot.loader.launch.JarLauncher
58+
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.loader.launch.JarLauncher
59+
```
60+
61+
To fix this error, you need to update the Dockerfile to use the `java` command to run the application.
62+
Update the ENTRYPOINT in the Dockerfile to use the following command:
63+
64+
```
65+
# ENTRYPOINT [ "java", "org.springframework.boot.loader.launch.JarLauncher" ]
66+
ENTRYPOINT [ "java", "org.springframework.boot.loader.JarLauncher" ]
67+
```
68+
69+
The auto-generated Dockerfile uses the `org.springframework.boot.loader.launch.JarLauncher` class to run the application. This class is not available in the Spring Boot 3.1.0 version which is used for this demo. You need to update the Dockerfile to use the `org.springframework.boot.loader.JarLauncher` class to run the application. If you are using higher version of Spring Boot, you may not need to update the Dockerfile.
70+
71+
The rerun the `docker compose up --build` command to build and run the application.
72+
73+
You should be able to access the application at `http://localhost:8080/greeting`
74+

0 commit comments

Comments
 (0)