DEV Community

Carlos A. Martinez
Carlos A. Martinez

Posted on

Hello world with Spring Boot and Docker

Image description

This is a small example of how to create a Docker container using Spring boot.

Here we go ...

  1. Create new Spring boot App spring initializr dependencies (spring web)

  2. Create new class HelloWorldController.java

@RestController
@RequestMapping("/api/v1/hello-world")
public class HelloWorldController {
    @GetMapping()
    public String getHelloWorld(){
        return "Hello everyone from Spring Boot App";
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Build jar file with maven

Image description

  1. Create Dockerfile in project root
FROM openjdk:11
ADD target/demo-hello-0.0.1-SNAPSHOT.jar demo-hello.jar
EXPOSE 8090
ENTRYPOINT ["java", "-jar", "demo-hello.jar"]
Enter fullscreen mode Exit fullscreen mode
  1. Generate imagen

>docker build -t image_hello .

  1. Generate container

> docker run --name hello_container -p 9000:8080 image_hello

  1. Test

localhost

Image description

Thank you so much for reading my post

Top comments (0)