Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couldn't connect to Docker Aerospike from host

I'm running aerospike server in docker.

$  docker run -d --name aerospike aerospike/aerospike-server
0ad3b2df67bd17f896e87ed119758d9af7fcdd9b82a8632828e01072e2c5673f

It is started successfully.

$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

I found the ip address of docker using below command.

$ docker inspect -f '{{.NetworkSettings.IPAddress }}' aerospike
172.17.0.2 

When I trying to connect to aql using the below command, it is successful as well.

$ 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)

Now I am trying to connect to the aerospike server in docker using java client in host machine.

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();
      }
  }

When I run the above program, I'm getting below error -

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)

I've tried both AerospikeClient("172.17.0.2", 3000) and AerospikeClient("localhost", 3000)

I see in the Dockerfile the port 3000 is exposed to the host but I'm not sure why I'm not able to use the aerospike server in the docker.

like image 465
Rajkumar Natarajan Avatar asked Nov 22 '17 21:11

Rajkumar Natarajan


1 Answers

The IP 172.17.0.2 is only accessible within Docker (therefore you can use another container to connect). In case you want to connect from your host you need to map the respective port.

docker run -d --name aerospike -p 3000:3000 aerospike/aerospike-server

Afterwards you can use:

AerospikeClient client = new AerospikeClient("localhost", 3000);
like image 55
Sebastian Avatar answered Nov 10 '22 12:11

Sebastian