Preston Price
Preston Price

Reputation: 325

Access Cassandra from separate docker container using docker-compose

I am trying to connect to a cassandra container from a separate container (named main).

This is my docker-compose.yml

version: '3.2' 
services:   
  main:
    build:
      context: .
    image: main-container:latest
    depends_on:
      - cassandra
    links:
      - cassandra
    stdin_open: true
    tty: true

  cassandra:
    build:
      context: .
      dockerfile: Dockerfile-cassandra
    ports:
      - "9042:9042"
      - "9160:9160"
    image: "customer-core-cassandra:latest"

Once I run this using docker-compose up, I run this command:

docker-compose exec main cqlsh cassandra 9042

but I get this error:

Connection error: ('Unable to connect to any servers', {'172.18.0.2': error(111, "Tried connecting to [('172.18.0.2', 9042)]. Last error: Connection refused")})

Upvotes: 3

Views: 1438

Answers (1)

Preston Price
Preston Price

Reputation: 325

I figured out the answer. Basically, in the cassandra.yaml file it sets the default rpc_address to localhost. If this is the case, Cassandra will only listen for requests on localhost, and will not allow connections from anywhere else. In order to change this, I had to set rpc_address to my "cassandra" container so my main container (and any other containers) could access Cassandra using the cassandra container ip address.

rpc_address: cassandra

Upvotes: 3

Related Questions