问题
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:
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused at redis.clients.jedis.Connection.connect(Connection.java:207) [jedis-2.9.0.jar:] at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93) [jedis-2.9.0.jar:] at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767) [jedis-2.9.0.jar:] at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106) [jedis-2.9.0.jar:] at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:888) [commons-pool2-2.4.3.jar:2.4.3] at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:432) [commons-pool2-2.4.3.jar:2.4.3] at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:361) [commons-pool2-2.4.3.jar:2.4.3] at redis.clients.util.Pool.getResource(Pool.java:49) [jedis-2.9.0.jar:] ... 27 more Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) [rt.jar:1.7.0_80] at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) [rt.jar:1.7.0_80] at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) [rt.jar:1.7.0_80] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) [rt.jar:1.7.0_80] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) [rt.jar:1.7.0_80] at java.net.Socket.connect(Socket.java:579) [rt.jar:1.7.0_80] at redis.clients.jedis.Connection.connect(Connection.java:184) [jedis-2.9.0.jar:] ... 34 more
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}'`
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.
回答1:
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.
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.
Also, make sure that there is an enty for parent-host in /etc/hosts inside the container.
来源:https://stackoverflow.com/questions/47529105/not-able-to-connect-to-redis-server-from-docker-container