javaspringdockerdocker-containerjib

Docker image using Jib plugin: Cant hit rest endpoint locally when container running?


I have a very simple spring boot web app that I created using spring initializer.

I have added the following controller:

@Controller
@RequestMapping("hello")
public class TestController {

    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> sayHello() {
        return ResponseEntity.ok("Hello!");
    }

}

When I run it locally in intelij I can hit the following endpoint successfully and get a response:

http://localhost:8080/hello

I have pushed the image created using jib to my docker hub registry and can pull the image successfully.

However when I run the container via docker as follows I get This site can’t be reached at the same URL.

docker run --name localJibExample123 -d -p 8080:80 bc2cbf3b85d1

I am able to run other containers ok and can hit the endpoints, what can be the issue here?

Running docker ps returns this for my running container:

"java -cp /app/resou…"   8 minutes ago    Up 8 minutes                  0.0.0.0:8080->80/tcp   localJibExample222

So it seems that my app should be accessible on:

http://localhost:8080/hello

Solution

  • Spring-boot, by default, runs on port 8080. The port mapping we are using in the docker run ... command maps the host's port 8080 to the container's port 80 (... -p 8080:80 ..., relevant docker run documentatn (docs.docker.com)). Since there is nothing running on the container's port 80, we get no response when accessing http://localhost:8080.

    The fix is straight-forward: we replace docker run ... -p 8080:80 ... with docker run ... -p 8080:8080 .... This will map to the container's port 8080, where the spring-boot application is listening. When we access http://localhost:8080 now, we will get a response.