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
2
Answers
I dont have much exp with yaml (currently learning myself), but looking at your yaml file seems port numbers don’t need to be in "" as it then appears as a string
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 cassandra
toDockerfile
Second change is to add
cassandra
toscript.sh
beforewhile
(it’s needed because when you added your script cassandra service wasn’t starting because you have overridedCMD
click 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
cassandra
container without creating newDockerfile
and he just wanted to access it from localhost, probably it was something like this usingdocker-compose.yaml
Then from localhost(not any container) you can access it using
cqlsh localhost 9042 -e 'describe cluster'
if you don’t havecqlsh
installed you can spin-up another container usingdocker run -it --rm --network host cassandra:latest bash
and inside type that commandTo achieve what you want you can add something like this
And one more change to
script.sh
we need add hostdd_cassandra
(name of the service/container) instead of usinglocalhost
After running
docker-compose up
and waiting around an half minute I got:First container
dd_cassandra
is just cassandara service, second containercassandra_script
has your script.sh inCMD
so it runs your script, the limitation here is thatDockerfile
can contain only oneCMD
so if it’s used more then once only last occurrence will be invoked.