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