问题描述
我的docker版本是1.13.1.我正在尝试从Docker容器连接到Redis服务器,但出现连接被拒绝错误.详细日志如下:
My docker version is 1.13.1.I am trying to connect to redis-server from my docker container,but I am getting connection refused error.Detailed logs are given below:
当我给出-net ="host"
选项时,此错误消失了,但是当我给出
when i give --net="host"
option this error is gone,but when i give
--add-host=parent-host:`ip route show | grep docker0 | awk '{print \$9}'`
我得到了错误,尽管容器能够连接到不同VM上的Db,但不能连接到自托管计算机上的redis-server.
I get the error,although the container is able to connect to Db on a different VM,but not connecting to redis-server present on self hosting machine.
推荐答案
我怀疑问题是您正在尝试通过 localhost
而不是 parent-host
.创建Jedis连接时,请确保通过 parent-host
连接到Redis.
I suspect the problem is that you are trying to connect to redis via localhost
rather than parent-host
. Make sure that you are connecting to redis via parent-host
when creating the Jedis connection.
-net = host
选项起作用的原因是,在这种情况下, localhost
将引用主机的IP,而不是容器的IP.
The reason --net=host
option works, is that in this case localhost
will refer to the IP of the host machine and not to that of the container.
此外,请确保容器内/etc/hosts
中的 parent-host
有一个实体.
Also, make sure that there is an enty for parent-host
in /etc/hosts
inside the container.
这篇关于无法从Docker容器连接到Redis服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!