Attila
Attila

Reputation: 1177

Can't connect to Cassandra Docker container from my Node.js app

I set up the Cassandra container by running the following commands:

This will pull the cassandra docker image from the docker hub and start up a container with cassandra

docker run --name cassandraDb -d --publish-all=true cassandra:3.11

I can see it set up by running the following commands:

docker images
docker ps -a
docker exec -i -t cassandraDb sh -c 'nodetool status'

Here is the code for my Node.js route, when I hit it, I get the proceeding error:

res.send(client.connect()
        .then(() => {
            console.log('Connected');
            console.log(Object.keys(client.metadata.keyspaces));
            return client.shutdown();
        })
        /* tslint:disable-next-line */
        .catch((err): any => {
            console.error('There was an error when connecting', err);
            return client.shutdown();
        }));

Error:

There was an error when connecting { [Error: All host(s) tried for query failed. First host tried, 172.17.0.2:9042: Error: Connection timeout. See innerErrors.]
  innerErrors:
   { '172.17.0.2:9042':
      { [Error: Connection timeout]
        message: 'Connection timeout',
        info: 'Cassandra Driver Error' } },
  info: 'Represents an error when a query cannot be performed because no host is available or could be reached by the driver.',
  message: 'All host(s) tried for query failed. First host tried, 172.17.0.2:9042: Error: Connection timeout. See innerErrors.' }

I don't have much experience with Cassandra or Docker, so I'm not sure why my app is not connecting to the DB, perhaps something with the ports not being open to incoming requests, but I don't know how I would check that or change it.

Upvotes: 2

Views: 2579

Answers (1)

Assen Kolov
Assen Kolov

Reputation: 4403

I see two problems here:

1) documentation says --publish-all=true|false .. is a blanket operation that identifies every port with an EXPOSE line in the image’s Dockerfile or --expose commandline flag and maps it to a host port somewhere within an ephemeral port range.

Instead --publish-all, which will map to random ports, do docker run --name cassandraDb -d -p 7199:7199 -p 7000:7000 -p 9042:9042 -p 9160:9160 -p7001:7001 cassandra:3.11. You can see the difference in docker ps output:

$ docker run --name cassandraDb -d -p 7199:7199 -p 7000:7000 -p 9042:9042 -p 
   9160:9160 -p7001:7001 cassandra:3.11 
$ docker ps
  ... PORTS   
      0.0.0.0:7000-7001->7000-7001/tcp, 0.0.0.0:7199->7199/tcp, 
      0.0.0.0:9042->9042/tcp, 0.0.0.0:9160->9160/tcp   cassandraDb

$ docker run --name cassandraDb -d --publish-all=true cassandra:3.11 
$ docker ps
... PORTS                                                                                                                         
    0.0.0.0:32782->7000/tcp, 0.0.0.0:32781->7001/tcp, 
    0.0.0.0:32780->7199/tcp, 0.0.0.0:32779->9042/tcp, 0.0.0.0:32778->9160/tcp 

2) Docker ip probably isn't 172.17.0.2. It is either localhost or try docker-machine ip

Upvotes: 2

Related Questions