вот файл:
- Dockerfile
Код: Выделить всё
FROM python:3.10-slim
WORKDIR /app
RUN pip install cassandra-driver
COPY init-db.cql /init-db.cql
COPY app.py /app/app.py
CMD ["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 Cluster
cluster = 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()
- создание сборки Docker
- docker compose up
когда я выполняю компоновку, появляется эта ошибка:Код: Выделить всё
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")})
Подробнее здесь: https://stackoverflow.com/questions/784 ... tavailable