8

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

Ankit Deshpande
  • 3,476
  • 1
  • 29
  • 42
Robert Ravikumar
  • 912
  • 2
  • 11
  • 29

6 Answers6

8

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

Pierre B.
  • 11,612
  • 1
  • 37
  • 58
7

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

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
Robert Ravikumar
  • 912
  • 2
  • 11
  • 29
1

I am able to run spring boot app with docker as below dockerfile :

FROM maven:3-alpine AS build-project
ADD . ./docker-spring-boot
WORKDIR /docker-spring-boot
RUN mvn clean install

FROM openjdk:8-jre-alpine
EXPOSE 8080
WORKDIR /app
COPY --from=build-project ./docker-spring-boot/target/docker-spring-boot-*.jar ./docker-spring-boot.jar
CMD ["java", "-jar", "docker-spring-boot.jar"]

For run command:

docker build -t docker-spring-boot .
docker run -it -d -p 8080:8080 docker-spring-boot

You need to replace docker-spring-boot with your jar file name located in target after build.

Emre Savcı
  • 3,034
  • 2
  • 16
  • 25
  • please check my code and find the mistake I am making – Robert Ravikumar Apr 07 '19 at 19:15
  • My jar name is docker-spring-boot.jar – Robert Ravikumar Apr 07 '19 at 19:23
  • Can you get into your container and send a curl request to verify that your application correctly run inside container? `docker exec -it containerId /bin/bash && curl http://localhost:8085` – Emre Savcı Apr 07 '19 at 19:27
  • root@a6664e1d3b83:/# curl http://localhost:8085/rest/docker/hello1 Greetingsroot@a6664e1d3b83:/# . I am able to get the response from the curl url . @RestController – Robert Ravikumar Apr 07 '19 at 19:38
  • It looks your application returns response as you see "Greeting". Did you try to send a request from your host machine? What error do you see? – Emre Savcı Apr 07 '19 at 19:41
  • response from host machine This site can’t be reached localhost refused to connect. Try: Checking the connection Checking the proxy and the firewall ERR_CONNECTION_REFUSED – Robert Ravikumar Apr 08 '19 at 00:46
0

Your code works well on any UNIX machine, the problem is related to the Docker network for the Windows version. By default when running docker use bridge network if no other is specified but in Windows the Docker network works in a different way, you can check the link here for more detail about the issue. This is a know bug but hopefully will be address some day by the Docker team.

Elton Stoneman in his blog will also give you a better explanation about this issue, see the following link.

ELavicount
  • 429
  • 3
  • 16
0

Its work for me. Here I am using docker Quick start terminal (Oracle Virtual Box) . I tried to find what is container IP by using 'docker-machine ip' command in quick start terminal. It gave me ip like 192.168.99.100. So then in browser I entered url like http://192.168.99.100:8080/hello1 and it gave me expected result. So first try to get the ip by using docker-machine ip command.

Gurudas
  • 1
  • 2
0

in the hopes that this might help someone avoid what i just endured: realized that i had updated my application.properties / yml with server.port: 8049 but my docker-compose file mapped ports - '8049:8080'. worked fine in the IDE but attempting to telnet to the docker container connected then immediately closed the connection. i'll take to the grave the number of hours i wasted trying to track this down...

slybuford
  • 1
  • 1