无法访问Docker容器映射到宿主上的端口

原创
2016/10/18 16:48
阅读数 3.1W

 现象

➜  ~ docker ps
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS                                            NAMES
3b45b6a8d0fc        elasticsearch:latest   "/bin/bash"         2 hours ago         Up About an hour    0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   ela-slave

端口是已经映射的

➜  ~ curl 127.0.0.1:9200
curl: (52) Empty reply from server

但是无法访问

原因:

docker里面的host不能配置127.0.0.1 或者192.168.0.1 或则宿主机器将无法访问端口

 

解决方案

修改 /usr/local/elasticsearch-2.4.1/config 参数

# network.host: 192.168.0.1
network.host: 0.0.0.0

将其改成0.0.0.0

启动后的效果如下

u should upgrade
[2016-10-18 06:48:05,655][INFO ][node                     ] [Wilson Fisk] version[2.4.1], pid[386], build[c67dc32/2016-09-27T18:57:55Z]
[2016-10-18 06:48:05,657][INFO ][node                     ] [Wilson Fisk] initializing ...
[2016-10-18 06:48:06,657][INFO ][plugins                  ] [Wilson Fisk] modules [reindex, lang-expression, lang-groovy], plugins [], sites []
[2016-10-18 06:48:06,707][INFO ][env                      ] [Wilson Fisk] using [1] data paths, mounts [[/ (none)]], net usable_space [54.9gb], net total_space [59gb], spins? [possibly], types [aufs]
[2016-10-18 06:48:06,709][INFO ][env                      ] [Wilson Fisk] heap size [1007.3mb], compressed ordinary object pointers [true]
[2016-10-18 06:48:09,754][INFO ][node                     ] [Wilson Fisk] initialized
[2016-10-18 06:48:09,755][INFO ][node                     ] [Wilson Fisk] starting ...
[2016-10-18 06:48:10,014][INFO ][transport                ] [Wilson Fisk] publish_address {172.17.0.3:9300}, bound_addresses {[::]:9300}
[2016-10-18 06:48:10,030][INFO ][discovery                ] [Wilson Fisk] elasticsearch/cbkE0IpITIGpLDXEQduQ1Q
[2016-10-18 06:48:13,193][INFO ][cluster.service          ] [Wilson Fisk] new_master {Wilson Fisk}{cbkE0IpITIGpLDXEQduQ1Q}{172.17.0.3}{172.17.0.3:9300}, reason: zen-disco-join(elected_as_master, [0] joins received)
[2016-10-18 06:48:13,276][INFO ][http                     ] [Wilson Fisk] publish_address {172.17.0.3:9200}, bound_addresses {[::]:9200}
[2016-10-18 06:48:13,277][INFO ][node                     ] [Wilson Fisk] started
[2016-10-18 06:48:13,342][INFO ][gateway                  ] [Wilson Fisk] recovered [0] indices into cluster_state

 可以再宿主机器正常访问

➜  ~ curl 127.0.0.1:9200
{
  "name" : "Wilson Fisk",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "xTeny0KFRHWNkzbqEIzNSg",
  "version" : {
    "number" : "2.4.1",
    "build_hash" : "c67dc32e24162035d18d6fe1e952c4cbcbe79d16",
    "build_timestamp" : "2016-09-27T18:57:55Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.2"
  },
  "tagline" : "You Know, for Search"
}

 

 

 

 

 

同理,我在docker里面配置了redis ,里面默认也是127.0.0.1  ,结果宿主机器无法访问docker端口

。这里根据上面的情况也将redis 配置成0.0.0.0

 

#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
#bind 127.0.0.1
bind 0.0.0.0
# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#

 

后来重启redis ,宿主机器也可以访问了

展开阅读全文
打赏
2
1 收藏
分享
加载中
非常感谢,帮助脱坑
2018/09/27 00:25
回复
举报
这个坑很重要
2018/02/17 14:04
回复
举报
把它忽略啦 感谢分享
2017/05/08 12:01
回复
举报
更多评论
打赏
3 评论
1 收藏
2
分享
返回顶部
顶部