kulsin
kulsin

Reputation: 458

Unable to load Spring Boot application running in Docker

I have created a simple Spring Boot v2.1.10 application with Gradle v5.6.4 and trying to run it in Docker v18.09.9, build 1752eb3. The application is working locally, but when I run it in Docker, the webpage doesn't load in the browser. Though, it is starting in the Docker console properly without any error.

Dockerfile content

FROM java:8
EXPOSE 9090:8181
ADD /build/libs/docker-app-0.0.1-SNAPSHOT.jar docker-app-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","docker-app-0.0.1-SNAPSHOT.jar"]

Springboot app controller

@RestController
public class Resource {

    @GetMapping(value = "/home")
    public String home() {
        return "<h1>up!!!.....running from docker!!!</h1>";
    }
}

Docker run command :~/java-project/docker-app$ sudo docker run -p 9090:8181 springboot

Docker console output

:~/java-project/docker-app$ sudo docker run -p 9090:8181 springboot

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.1.10.RELEASE)

2019-11-19 20:38:03.870  INFO 1 --- [           main] com.DockerAppApplication                 : Starting DockerAppApplication on 93febc16ff7d with PID 1 (/docker-app-0.0.1-SNAPSHOT.jar started by root in /)
2019-11-19 20:38:03.877  INFO 1 --- [           main] com.DockerAppApplication                 : No active profile set, falling back to default profiles: default
2019-11-19 20:38:06.459  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9090 (http)
2019-11-19 20:38:06.528  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-11-19 20:38:06.528  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-19 20:38:06.669  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-11-19 20:38:06.669  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2642 ms
2019-11-19 20:38:07.012  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-19 20:38:07.333  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9090 (http) with context path ''
2019-11-19 20:38:07.339  INFO 1 --- [           main] com.DockerAppApplication                 : Started DockerAppApplication in 4.276 seconds (JVM running for 5.029)

NOTE: When I give the same port number(sudo docker run -p 9090:9090 springboot) in run command and in Dockerfile, then it's working properly.

What's different in case of different port numbers in the docker run command?

Upvotes: -1

Views: 1778

Answers (1)

user4695271
user4695271

Reputation:

TL;DR

Change your mapping to -p 8181:9090


You got that the wrong way. The -p 9090:8181 setting will expose the container's port 8181 on the host's port 9090 — assuming that port is not in use. You can effectively use the port 9090 on the host, but the Spring Boot application in the container is not listening on 8181, so you won't get any response back from it.

When you do -p 9090:9090 you are correctly exposing 9090 on the host and the container; again, the Spring Boot application is effectively listening on port 9090, so you are able hit the Spring Boot application and get something back.

In any case, what you are looking for is -p 8181:9090. That will let you use the application on port 8181 in the browser/host, while the Spring Boot application uses 9090.

Upvotes: 5

Related Questions