无法从主机连接到Docker Aerospike
我正在docker中运行aerospike服务器。
$ docker run -d --name aerospike aerospike/aerospike-server 0ad3b2df67bd17f896e87ed119758d9af7fcdd9b82a8632828e01072e2c5673f
它启动成功。
$docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0ad3b2df67bd aerospike/aerospike-server "/entrypoint.sh asd" 4 seconds ago Up 2 seconds 3000-3003/tcp aerospike
我使用下面的命令find了docker的ip地址。
$ docker inspect -f '{{.NetworkSettings.IPAddress }}' aerospike 172.17.0.2
当我尝试使用下面的命令连接到aql时,它也是成功的。
$ docker run -it aerospike/aerospike-tools aql -h $(docker inspect -f '{{.NetworkSettings.IPAddress }}' aerospike) Aerospike Query Client Version 3.15.0.3 C Client Version 4.2.0 Copyright 2012-2017 Aerospike. All rights reserved. aql> select * from test.person 0 rows in set (0.002 secs)
现在我正在尝试使用主机中的java客户端连接到docker中的aerospike服务器。
public class AerospikeDemo { public static void main(String []args) { AerospikeClient client = new AerospikeClient("172.17.0.2", 3000); Key key = new Key("test", "demo", "putgetkey"); //Key key2 = new Key("1", "2", "3"); Bin bin1 = new Bin("bin1", "value1"); Bin bin2 = new Bin("bin2", "value2"); Bin bin3 = new Bin("bin2", "value3"); // Write a record client.put(null, key, bin1, bin2, bin3); // Read a record Record record = client.get(null, key); System.out.println("record is "+ record); System.out.println("record bins is " + record.bins); client.close(); } }
当我运行上面的程序,我得到以下错误 –
objc[3446]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java (0x10f7b14c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10f8794e0). One of the two will be used. Which one is undefined. Exception in thread "main" com.aerospike.client.AerospikeException$Connection: Error Code 11: Failed to connect to host(s): 172.17.0.2 3000 Error Code 11: java.net.SocketTimeoutException: connect timed out at com.aerospike.client.cluster.Cluster.seedNodes(Cluster.java:413) at com.aerospike.client.cluster.Cluster.tend(Cluster.java:306) at com.aerospike.client.cluster.Cluster.waitTillStabilized(Cluster.java:271) at com.aerospike.client.cluster.Cluster.initTendThread(Cluster.java:181) at com.aerospike.client.AerospikeClient.<init>(AerospikeClient.java:210) at com.aerospike.client.AerospikeClient.<init>(AerospikeClient.java:151) at com.demo.aerospike.AerospikeDemo.main(AerospikeDemo.java:12)
我试过AerospikeClient("172.17.0.2", 3000)
和AerospikeClient("localhost", 3000)
我在Dockerfile中看到端口3000暴露给主机,但我不知道为什么我不能在docker中使用aerospike服务器。
IP 172.17.0.2
只能在Docker中访问(因此可以使用另一个容器来连接)。 如果你想从你的主机连接,你需要映射各自的端口。
docker run -d --name aerospike -p 3000:3000 aerospike/aerospike-server
之后你可以使用:
AerospikeClient client = new AerospikeClient("localhost", 3000);