Unable to cqlsh to a cassandra docker container remotely

40 Views Asked by At

Here's the docker-compose.yml file:

version: "3"
services:
  db_cassandra:
    container_name: db_cassandra
    image: custom.cassandra.image/builder-cassandra
    volumes:
      - "./common/cassandra:/lua/cassandra_setup:rw"
    environment:
      WORKSPACE: "/tmp"
      SERVICES_ROOT_DIR: "/services_root"
    healthcheck:
        test: ["CMD", "cqlsh", "-u", "cassandra", "-p", "cassandra" ]
        interval: 5s
        timeout: 5s
        retries: 60
    ports:
      - "9042:9042"

  remote_cassandra:
    container_name: remote_cassandra
    build:
      context: ../.
      dockerfile: ./it/remote_cassandra/Dockerfile
      args:
        BASE_IMAGE: custom.cassandra.image/builder-cassandra
    depends_on:
      dd_cassandra:
        condition: service_healthy
    volumes:
      - "./common/cassandra:/lua/cassandra_setup:rw"
    environment:
      WORKSPACE: "/tmp"
      SERVICES_ROOT_DIR: "/services_root"

Here's the remote_cassandra/Dockerfile:

ARG BASE_IMAGE
FROM ${BASE_IMAGE}

COPY ./it/common/cassandra/cassandra-setup.sh /
RUN chmod +x /cassandra-setup.sh

CMD ["/cassandra-setup.sh"]

remote_cassandra remotely connects to the db_cassandra service and executes certain queries.

Here's how the cassandra-setup.sh script looks like:

#!/bin/bash
#code that creates schema.cql
.
.
.
while ! cqlsh db_cassandra -e 'describe cluster' ; do
  echo "waiting for db_cassandra to be up..."
  sleep .5
done
cqlsh db_cassandra -f "${WORKSPACE}/schema.cql"

The while loop makes remote_cassandra wait until the db_cassandra is up and running so that it can then run the schema.cql remotely to populate certain tables present in db_cassandra.

However, the above script runs into an infinite loop where remote_cassandra is unable to remotely cqlsh to db_cassandra.

Here are the logs:

Traceback (most recent call last):
  File "/opt/cassandra/bin/cqlsh.py", line 2357, in <module>
    main(*read_options(sys.argv[1:], os.environ))
  File "/opt/cassandra/bin/cqlsh.py", line 2303, in main
    shell = Shell(hostname,
  File "/opt/cassandra/bin/cqlsh.py", line 463, in __init__
    load_balancing_policy=WhiteListRoundRobinPolicy([self.hostname]),
  File "/opt/cassandra/bin/../lib/cassandra-driver-internal-only-3.25.0.zip/cassandra-driver-3.25.0/cassandra/policies.py", line 425, in __init__
  File "/opt/cassandra/bin/../lib/cassandra-driver-internal-only-3.25.0.zip/cassandra-driver-3.25.0/cassandra/policies.py", line 426, in <listcomp>
  File "/usr/lib/python3.10/socket.py", line 955, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
waiting for db_cassandra to be up...

The above logs keeps getting printed again and again.

What can be done here to form a connection?

EDIT: I'm trying to pass the name of db_cassandra as cli arg to bash script - cassandra-setup.sh. However, when running docker-compose, it takes forever for the container to run.

Here's how I'm passing the arg:

ENTRYPOINT ["/cassandra-setup.sh"]
CMD ["db_cassandra"]

and then cqlsh(ing) it like:

#!/bin/bash
DB_CONTAINER="$1"
#code that creates schema.cql
.
.
.
while ! cqlsh $DB_CONTAINER -e 'describe cluster' ; do
  echo "waiting for db_cassandra to be up..."
  sleep .5
done
cqlsh $DB_CONTAINER -f "${WORKSPACE}/schema.cql"

This somehow just creates both the cassandra and the execution seems to be stuck forever like this.

1

There are 1 best solutions below

6
datawookie On

I don't have access to the custom.cassandra.image/builder-cassandra image so just using cassandra:latest in the db_cassandra service.

Here's a minimal setup that does what you require.

├── cassandra-setup.sh
├── docker-compose.yml
└── Dockerfile

docker-compose.yml (Based on the docker-compose.yml but stripped down to minimal requirements.)

version: "3"
services:
  db_cassandra:
    container_name: db_cassandra
    image: cassandra:latest
    environment:
      WORKSPACE: "/tmp"
      SERVICES_ROOT_DIR: "/services_root"
    healthcheck:
        test: ["CMD", "cqlsh", "-u", "cassandra", "-p", "cassandra" ]
        interval: 5s
        timeout: 5s
        retries: 60
    ports:
      - "9042:9042"
    logging:
      driver: none

  remote_cassandra:
    container_name: remote_cassandra
    build:
      context: .

Dockerfile (I'm not sure what you are specifying for BASE_IMAGE so I used cassandra:latest.)

FROM cassandra:latest

COPY cassandra-setup.sh /
RUN chmod +x /cassandra-setup.sh

CMD ["/cassandra-setup.sh"]

cassandra-setup.sh

#!/bin/bash

echo " Starting setup."

until cqlsh -e "describe cluster" db_cassandra 9042 >/dev/null 2>&1; do
  echo " Waiting for Cassandra to be ready."
  sleep 10
done

echo " Cassandra is ready. Describe the keyspaces."
cqlsh -e "describe keyspaces" db_cassandra 9042

echo " Done!"

enter image description here