无法使用Spring Boot应用程序连接到Docker映像

时间:2019-04-07 18:34:59

标签: java spring spring-boot docker containers

Dockerfile

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

构建docker的命令

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

下方的弹簧靴子位置。

target/docker-spring-boot.jar

运行docker的命令

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

该应用程序在没有docker的情况下正常运行。无法在docker上运行应用程序。

错误:

This site can’t be reached

docker运行日志

 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链接 https://github.com/robert07ravikumar/spring-boot-docker

enter image description here

5 个答案:

答案 0 :(得分:4)

我尝试使用的URL是localhost127.0.0.10.0.0.0等。

一旦我运行了docker主机URL,该应用程序便开始工作:

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

答案 1 :(得分:2)

您的应用程序看起来不错,看来您可以根据自己的评论使用本地主机从容器内的访问它

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

可能是因为默认情况下,Spring Boot将绑定到本地主机(127.0.0.1)。您需要添加以下属性以绑定到所有主机(或指定要绑定的IP):

server.address=0.0.0.0 # Bind all

在您的application.properties

请参见this postSpring Boot Common Properties

答案 2 :(得分:0)

我能够使用以下dockerfile与docker运行spring boot应用程序:

cmdscale

对于运行命令:

wish

构建后,需要用位于目标中的jar文件名替换 docker-spring-boot

答案 3 :(得分:0)

您的代码在任何 UNIX 计算机上都能正常运行,问题与Windows版本的Docker网络有关。默认情况下,运行docker时使用 bridge 网络(如果未指定其他网络),但在Windows中Docker网络以不同的方式工作,您可以检查链接here以获得有关该问题的更多详细信息。这是一个已知的错误,但是希望有一天可以由Docker团队解决。

Elton Stoneman在他的博客中还将为您提供有关此问题的更好解释,请参见以下link

答案 4 :(得分:0)

对我有用。在这里,我正在使用docker快速入门终端(Oracle Virtual Box)。我试图通过在快速启动终端中使用“ docker-machine ip”命令来查找什么是容器IP。它给我的IP就像192.168.99.100。因此,然后在浏览器中输入了http://192.168.99.100:8080/hello1之类的网址,它给了我预期的结果。因此,首先尝试使用docker-machine ip命令获取IP。