I'm running a cassandra container with the the help of docker. For the below set of configuration I'm trying to run the script.sh script which should run after cassandra is up and running. However, the below script keeps logging ConnectionRefusedError(111) error:
dd_cassandra | Connection error: ('Unable to connect to any servers', {'127.0.0.1:9042': ConnectionRefusedError(111, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")})
dd_cassandra | Waiting for cassandra-node-1...
I used the solution described in this SO Post to check further execute the script only if cassandra is ready but it runs into an infinite wait.
docker-compose.yml
version: '3'
services:
dd_cassandra:
container_name: dd_cassandra
build:
context: ./cassandra
dockerfile: Dockerfile
args:
BASE_IMAGE: cassandra:latest
ports:
- "9042:9042"
cassandra/Dockerfile
ARG BASE_IMAGE
FROM ${BASE_IMAGE}
COPY script.sh /
RUN chmod +x /script.sh
EXPOSE 9042
CMD ["/script.sh"]
cassandra/script.sh
#!/bin/bash
while ! cqlsh -e 'describe cluster' ; do
echo "Waiting for cassandra-node-1...";
sleep 10
done
# some other piece of code that adds runs `cqlsh` command
# in the cassandra database
Quick fix
What you are doing is not exactly correct how containers should be used, but to just fix it you need to add
USER cassandratoDockerfileSecond change is to add
cassandratoscript.shbeforewhile(it's needed because when you added your script cassandra service wasn't starting because you have overridedCMDclick here and scroll to 'CMD ["cassandra" "-f"]')This works as I tested.
More appropriate approach
I suggest you doing it a little bit differently. Looking into How to check that a Cassandra node is ready? someone started
cassandracontainer without creating newDockerfileand he just wanted to access it from localhost, probably it was something like this usingdocker-compose.yamlThen from localhost(not any container) you can access it using
cqlsh localhost 9042 -e 'describe cluster'if you don't havecqlshinstalled you can spin-up another container usingdocker run -it --rm --network host cassandra:latest bashand inside type that commandTo achieve what you want you can add something like this
And one more change to
script.shwe need add hostdd_cassandra(name of the service/container) instead of usinglocalhostAfter running
docker-compose upand waiting around an half minute I got:First container
dd_cassandrais just cassandara service, second containercassandra_scripthas your script.sh inCMDso it runs your script, the limitation here is thatDockerfilecan contain only oneCMDso if it's used more then once only last occurrence will be invoked.