Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to connect to docker image with a spring boot application

Dockerfile

FROM openjdk:8 
ADD target/docker-spring-boot.jar docker-spring-boot.jar
EXPOSE 8085
ENTRYPOINT ["java","-jar","docker-spring-boot.jar"]

command to build docker

docker build -f Dockerfile -t docker-spring-boot .

spring boot jar location below .

target/docker-spring-boot.jar

command to run docker

docker run -p 8085:8085 docker-spring-boot

The application works normally without a docker. Not able to run the application on docker.

Error:

This site can’t be reached

docker run logs

 E:\micorservices_samples\docker-spring-boot\docker-springbootdocker run -p 8085 :8085 docker-spring-boot

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

 2019-04-07 18:29:47.944  INFO 1 --- [           main]
 c.r.d.DockerSpringbootAppl ication        : Starting
 DockerSpringbootApplication v0.0.1-SNAPSHOT on 352ac4d 12009 with PID
 1 (/docker-spring-boot.jar started by root in /) 2019-04-07
 18:29:47.970  INFO 1 --- [main] c.r.d.DockerSpringbootAppl
 ication        : No active profile set, falling back to default
 profiles: defaul t 2019-04-07 18:29:54.302  INFO 1 --- [          
 main] o.s.b.w.embedded.tomcat.To mcatWebServer  : Tomcat initialized
 with port(s): 8085 (http) 2019-04-07 18:29:54.463  INFO 1 --- [       
 main] o.apache.catalina.core.Sta ndardService   : Starting service
 [Tomcat] 2019-04-07 18:29:54.464  INFO 1 --- [           main]
 org.apache.catalina.core.S tandardEngine  : Starting Servlet engine:
 [Apache Tomcat/9.0.17] 2019-04-07 18:29:54.854  INFO 1 --- [          
 main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring
 embedded WebApplicationContext 2019-04-07 18:29:54.855  INFO 1 --- [  
 main] o.s.web.context.ContextLoa der            : Root
 WebApplicationContext: initialization completed in 6539 ms 2019-04-07
 18:29:55.859  INFO 1 --- [main] o.s.s.concurrent.ThreadPoo
 lTaskExecutor  : Initializing ExecutorService
 'applicationTaskExecutor' 2019-04-07 18:29:56.691  INFO 1 --- [       
 main] o.s.b.w.embedded.tomcat.To mcatWebServer  : Tomcat started on
 port(s): 8085 (http) with context path '' 2019-04-07 18:29:56.705 
 INFO 1 --- [main] c.r.d.DockerSpringbootAppl ication       
 : Started DockerSpringbootApplication in 10.902 seconds (JVM runn ing
 for 12.566)



 C:\Users\Mabeldocker logs -f 20026c6c7602

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

 2019-04-07 18:51:23.381  INFO 1 --- [           main]
 c.r.d.DockerSpringbootAppl ication        : Starting
 DockerSpringbootApplication v0.0.1-SNAPSHOT on 20026c6 c7602 with PID
 1 (/dsb.jar started by root in /) 2019-04-07 18:51:23.403  INFO 1 ---
 [           main] c.r.d.DockerSpringbootAppl ication        : No
 active profile set, falling back to default profiles: defaul t
 2019-04-07 18:51:29.434  INFO 1 --- [main]
 o.s.b.w.embedded.tomcat.To mcatWebServer  : Tomcat initialized with
 port(s): 8085 (http) 2019-04-07 18:51:29.608  INFO 1 --- [          
 main] o.apache.catalina.core.Sta ndardService   : Starting service
 [Tomcat] 2019-04-07 18:51:29.613  INFO 1 --- [           main]
 org.apache.catalina.core.S tandardEngine  : Starting Servlet engine:
 [Apache Tomcat/9.0.17] 2019-04-07 18:51:30.012  INFO 1 --- [          
 main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring
 embedded WebApplicationContext 2019-04-07 18:51:30.014  INFO 1 --- [  
 main] o.s.web.context.ContextLoa der            : Root
 WebApplicationContext: initialization completed in 6275 ms 2019-04-07
 18:51:31.038  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoo
 lTaskExecutor  : Initializing ExecutorService
 'applicationTaskExecutor' 2019-04-07 18:51:31.879  INFO 1 --- [       
 main] o.s.b.w.embedded.tomcat.To mcatWebServer  : Tomcat started on
 port(s): 8085 (http) with context path '' 2019-04-07 18:51:31.895 
 INFO 1 --- [           main] c.r.d.DockerSpringbootAppl ication       
 : Started DockerSpringbootApplication in 10.631 seconds (JVM runn ing
 for 12.241)

Github link https://github.com/robert07ravikumar/spring-boot-docker

enter image description here

like image 645
Robert Ravikumar Avatar asked Apr 07 '19 18:04

Robert Ravikumar


2 Answers

Your application seems fine, and it seems you can access it from within the container with localhost as per your comment:

root@a6664e1d3b83:/# curl localhost:8085/rest/docker/hello1 
Greetings
root@a6664e1d3b83:/# . I am able to get the response from the curl url 

It's probably because Spring Boot will bind to localhost by default (127.0.0.1). You need to add the following properties to bind to all host (or specify which IP on which to bind):

server.address=0.0.0.0 # Bind all

In your application.properties

See this post and the Spring Boot Common Properties

like image 73
Pierre B. Avatar answered Nov 06 '22 02:11

Pierre B.


The URLs I was trying was localhost , 127.0.0.1 , 0.0.0.0, etc .

The application worked once I ran the docker host URL:

http://192.168.99.100:8085/rest/docker/hello1

like image 26
Robert Ravikumar Avatar answered Nov 06 '22 02:11

Robert Ravikumar