docker容器启动后无法访问宿主机host

项目场景:

项目相关背景:

java项目,SpringCloud框架,Consul注册中心。
Consul注册中心部署在服务器物理机了,其中在增加了 host配置 127.0.0.1 consul,也就是说consul:8500就可以访问。
SpringCloud项目yml配置文件中的注册中心host配置的是 consul。
环境:centos服务器,docker部署。


问题描述

项目中遇到的问题:
前奏工作已准备好(打包),在启动容器时,发现启动失败,原因是java服务启动时无法连接到注册中心,也就是在容器中无法访问物理机的host。
在这里插入图片描述

2022-03-12 17:22:17.783 ERROR [main     ] [org.springframework.cloud.consul.config.ConsulPropertySourceLocator][147] Fail fast is set and there was an error reading configuration from consul.
2022-03-12 17:22:18.785 ERROR [main     ] [org.springframework.cloud.consul.config.ConsulPropertySourceLocator][147] Fail fast is set and there was an error reading configuration from consul.
2022-03-12 17:22:19.891 ERROR [main     ] [org.springframework.cloud.consul.config.ConsulPropertySourceLocator][147] Fail fast is set and there was an error reading configuration from consul.
2022-03-12 17:22:21.103 ERROR [main     ] [org.springframework.cloud.consul.config.ConsulPropertySourceLocator][147] Fail fast is set and there was an error reading configuration from consul.
2022-03-12 17:22:22.436 ERROR [main     ] [org.springframework.cloud.consul.config.ConsulPropertySourceLocator][147] Fail fast is set and there was an error reading configuration from consul.
2022-03-12 17:22:23.903 ERROR [main     ] [org.springframework.cloud.consul.config.ConsulPropertySourceLocator][147] Fail fast is set and there was an error reading configuration from consul.
2022-03-12 17:22:23.913 ERROR [main     ] [org.springframework.boot.SpringApplication][822] Application run failed
com.ecwid.consul.transport.TransportException: java.net.UnknownHostException: consul
        at com.ecwid.consul.transport.AbstractHttpTransport.executeRequest(AbstractHttpTransport.java:77)
        at com.ecwid.consul.transport.AbstractHttpTransport.makeGetRequest(AbstractHttpTransport.java:34)
        at com.ecwid.consul.v1.ConsulRawClient.makeGetRequest(ConsulRawClient.java:128)
        at com.ecwid.consul.v1.kv.KeyValueConsulClient.getKVValues(KeyValueConsulClient.java:150)
        at com.ecwid.consul.v1.ConsulClient.getKVValues(ConsulClient.java:534)
        at org.springframework.cloud.consul.config.ConsulPropertySource.init(ConsulPropertySource.java:67)
        at org.springframework.cloud.consul.config.ConsulPropertySourceLocator.create(ConsulPropertySourceLocator.java:179)
        at org.springframework.cloud.consul.config.ConsulPropertySourceLocator.locate(ConsulPropertySourceLocator.java:139)
        at org.springframework.cloud.consul.config.ConsulPropertySourceLocator$$FastClassBySpringCGLIB$$b35ebf8.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
        at org.springframework.retry.interceptor.RetryOperationsInterceptor$1.doWithRetry(RetryOperationsInterceptor.java:91)
        at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:287)
        at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:164)
        at org.springframework.retry.interceptor.RetryOperationsInterceptor.invoke(RetryOperationsInterceptor.java:118)
        at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:153)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
        at org.springframework.cloud.consul.config.ConsulPropertySourceLocator$$EnhancerBySpringCGLIB$$35b619c3.locate(<generated>)
        at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:97)
        at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:622)
        at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:366)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203)
        at com.yudao.data.compute.ServiceApplication.main(ServiceApplication.java:25)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: java.net.UnknownHostException: consul
        at java.net.InetAddress.getAllByName0(InetAddress.java:1281)
        at java.net.InetAddress.getAllByName(InetAddress.java:1193)
        at java.net.InetAddress.getAllByName(InetAddress.java:1127)
        at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
        at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:112)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:374)
        at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:221)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:165)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:140)
        at com.ecwid.consul.transport.AbstractHttpTransport.executeRequest(AbstractHttpTransport.java:61)
        ... 34 common frames omitted

原因分析:

Docker容器运行的时候有 host 、 bridge 、 none 三种网络可供配置。默认是 bridge ,即桥接网络,以桥接模式连接到宿主机; host 是宿主网络,即与宿主机共用网络; none 则表示无网络,容器将无法联网。

当容器使用 host 网络时,容器与宿主共用网络,这样就能在容器中访问宿主机网络,那么容器的 consul 就是宿主机的 consul 。


解决方案:

只要让容器使用宿主机的host网络即可。

在docker中使用 --network host 来为容器配置 host 网络:
docker run --name data-service --network host -d data-service:latest

结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值