Reputation: 1751
I have a golang service that run in a container, along with a cassandra
db.
This is running like this :
services:
cassandra:
image: cassandra:4.0
...
healthcheck:
test: ["CMD-SHELL", "[ $$(nodetool statusgossip) = running ]"]
interval: 30s
timeout: 10s
retries: 5
my_service:
...
command: ./my_service
depends_on:
cassandra:
condition: service_healthy
environment:
...
with a dockerfile like this
# syntax = docker/dockerfile:experimental
FROM ...
COPY ./bin/reply-by-email .
ENTRYPOINT ["./my_service"]
In the code I connect to the DB while specifying the keyspace
that I created manually.
But I need to have the keyspace created automatically when I perform the docker-compose up
Do you have any idea how to perform this ?
Upvotes: 3
Views: 4216
Reputation: 153
I did it this way:
init.sh
:#!/usr/bin/env bash
until printf "" 2>>/dev/null >>/dev/tcp/cassandra/9042; do
sleep 5;
echo "Waiting for cassandra...";
done
echo "Creating keyspace and table..."
cqlsh cassandra -u cassandra -p cassandra -e "CREATE KEYSPACE IF NOT EXISTS test WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'};"
cqlsh cassandra -u cassandra -p cassandra -e "CREATE TABLE IF NOT EXISTS test.test (sensor_id uuid, registered_at timestamp, temperature int, PRIMARY KEY ((sensor_id), registered_at));"
init:
image: cassandra:4
depends_on:
- cassandra
restart: "no"
entrypoint: ["/init.sh"]
volumes:
- ./init.sh:/init.sh
It works as a charm, full implementation is here if you need real code.
Upvotes: 2