Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

Running Cassandra on Docker with some Python operation returns NoHostAvailable, ConnectionRefusedError

$
0
0

i want to create in local a cassandra db with docker , and using a python script that insert some data and print it

there are the file :

  • Dockerfile
FROM python:3.10-slimWORKDIR /appRUN pip install cassandra-driverCOPY init-db.cql /init-db.cqlCOPY app.py /app/app.pyCMD ["python", "app.py"]
  • docker-compose.yml
version: '3'services:  cassandra:    image: cassandra:latest    container_name: cassandra    ports:      - "9042:9042"    volumes:      - ./init-db.cql:/init-db.cql    command: ["cassandra", "-f"]  app:    build:      context: .      dockerfile: Dockerfile    container_name: app    depends_on:      - cassandra    links:      - cassandra    volumes:      - ./app.py:/app/app.py
  • init-db.cql
CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'};CREATE TABLE IF NOT EXISTS mytable (    id UUID PRIMARY KEY,    message TEXT);INSERT INTO mytable (id, message) VALUES ('1',"hello i am the first");INSERT INTO mytable (id, message) VALUES ('2',"hello i am the second");
  • app.py
from cassandra.cluster import Clustercluster = Cluster(['cassandra'])session = cluster.connect('mykeyspace')session.execute("INSERT INTO mytable (id, message) VALUES (uuid(), 'ciao mondo sono il terzo')")session.execute("INSERT INTO mytable (id, message) VALUES (uuid(), 'ciao mondo sono il quarto')")result = session.execute("SELECT * FROM mytable")for row in result:    print(row.id, row.message)cluster.shutdown()

i do this command to execute:

  • docker compose build
  • docker compose upwhen i execute the compose up, it come out with this error:cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'172.23.0.2:9042': ConnectionRefusedError(111, "Tried connecting to [('172.23.0.2', 9042)]. Last error: Connection refused")})

i tried to look the documentation


Viewing all articles
Browse latest Browse all 23276

Trending Articles