Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create keyspace on startup #104

Closed
ghost opened this issue Apr 17, 2017 · 17 comments
Closed

Create keyspace on startup #104

ghost opened this issue Apr 17, 2017 · 17 comments

Comments

@ghost
Copy link

ghost commented Apr 17, 2017

Is there any way to copy a .cql script into the cassandra image which gets automatically executed as soon as cassandra is fully initialized and available on port 9042?

@rodrigorodrigues
Copy link

Hi all,

I have the same issue, is there one way to load a script after cassandra is ready?

I'm using Vagrant + docker-compose on Win 7.

@SpinGit
Copy link

SpinGit commented Aug 5, 2017

Hi i have the same issue as well. I would like to configure a keyspace for my integration tests.

I'm using bitbucket pipeline and i have the following setup:

  default:
    - step:
        script:
          - sleep 15
          - npm test
        services:
          - cassandra
definitions:
  services:
    cassandra:
      image: cassandra
      environment:
        MAX_HEAP_SIZE: 512M # Need to restrict the heapsize or else Cassandra will OOM
        HEAP_NEWSIZE: 128M

I would like to have an environment variable to set the default keyspace, with an default strategey. Otherwise provisioning of an running cassandra db must be done with another docker container...

Thank you very much - Not sure if this issue is a problem of the docker image itself.

@ghost
Copy link
Author

ghost commented Aug 5, 2017

Hey guys, hope this helps ;-)

https://github.com/dschroe/cassandra-docker

@MatteoJoliveau
Copy link

@dschroe can you add a README with a short example about how to run your image?

@Chabane
Copy link

Chabane commented Sep 22, 2017

@dschroe not working, the CQL scripts are never executed

@ghost
Copy link
Author

ghost commented Sep 22, 2017

Can you share your configuration pls?

My dummy-configuration (docker-compose and .cql file) looks like this:

cassandra-db:
    image: dschroe/cassandra-docker
    restart: always
    volumes:
      - /cassandra:/var/lib/cassandra
      - ./db-schema:/docker-entrypoint-initdb.d/
CREATE KEYSPACE IF NOT EXISTS kspace WITH replication = 
{'class':'SimpleStrategy','replication_factor':'1'};

CREATE TABLE IF NOT EXISTS kspace.model
(
  timestamp timestamp,
  id varchar,
  PRIMARY KEY (id, timestamp)

) WITH CLUSTERING ORDER BY (timestamp DESC);

and it works fine for me.

@Chabane
Copy link

Chabane commented Sep 22, 2017

@dschroe Thanks for sharing!
If you put your configuration in the readme, I think the vote would be a more reasonable ;-)

It works with this solution https://stackoverflow.com/a/46037377

@daggerok
Copy link

daggerok commented Oct 6, 2017

I cannot use image: dschroe/cassandra-docker from dokerhub, but I can build from Dockerfile of repository @dschroe provided, ie:

git clone https://github.com/dschroe/cassandra-docker .

docker-compose.yml

version: "2.1"

services:

  cassadnra:
    image: 127.0.0.1:5000/cassandra
    build: .
    restart: unless-stopped
    ports: ["9042:9042"]
    volumes:
      - cassandra-data:/var/lib/cassandra
      - cassandra-data:/home/cassandra/.cassandra
      - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
    networks: [backing-services]

volumes:
  cassandra-data: {}

networks:
  backing-services:

docker-entrypoint-initdb.d/init.cql

CREATE KEYSPACE IF NOT EXISTS demo WITH replication = 
{'class':'SimpleStrategy','replication_factor':'1'};

and then

docker-compose up --build
# ...
cassadnra_1  | INFO  [Native-Transport-Requests-2] 2017-10-06 20:59:03,847 MigrationManager.java:310 - Create new Keyspace: KeyspaceMetadata{name=demo, ...

see fork


Regards,
Maksim

@hantsy
Copy link

hantsy commented Jan 18, 2018

@daggerok I am using DockerToolbox under Windows 10, it seems the ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d volume mapping does not work.

@daggerok
Copy link

daggerok commented Jan 18, 2018

Hi @hantsy,

unfortunately I have not tested it using toolbox on Windows 10
verified on Docker CE on OSX and Docker on Linux ubuntu


Regards

@moharnab123saikia
Copy link

@dschroe Your script worked for me on OSX. Thanks.

@tianon
Copy link
Member

tianon commented Apr 20, 2018

Closing in favor of #104 (comment) / https://stackoverflow.com/a/46037377/433558:

FROM cassandra

COPY entrypoint-wrap.sh /entrypoint-wrap.sh
ENTRYPOINT ["/entrypoint-wrap.sh"]
CMD ["cassandra", "-f"]
#!/bin/bash

if [[ ! -z "$CASSANDRA_KEYSPACE" && $1 = 'cassandra' ]]; then
  # Create default keyspace for single node cluster
  CQL="CREATE KEYSPACE $CASSANDRA_KEYSPACE WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};"
  until echo $CQL | cqlsh; do
    echo "cqlsh: Cassandra is unavailable - retry later"
    sleep 2
  done &
fi

exec /docker-entrypoint.sh "$@"

👍

@tianon tianon closed this as completed Apr 20, 2018
@aisven
Copy link

aisven commented May 7, 2018

It is a very common use-case in software development. I do not like the workaround, because it means so many people now need to build their own C* Docker Images.

I would much prefer to see support for this Issue in the official Docker image.

@veysiertekin
Copy link

I was able to initialize Cassandra without a new Docker image by inspiring above comments.

I have added volumes and command sections to my compose.yml file:

version: '2.1'
...
  cassandra:
    image: cassandra:3.11.2
    volumes:
      - "./cassandra-init.sh:/cassandra-init.sh"
    command: "sh /cassandra-init.sh"
...

But called docker-entrypoint.sh at the end of cassandra-init.sh.
The contents of cassandra-init.sh:

cat >/import.cql <<EOF
DROP keyspace test;
CREATE keyspace test with replication = {'class':'SimpleStrategy', 'replication_factor' : 1};
EOF

# You may add some other conditionals that fits your stuation here
until cqlsh -f /import.cql; do
  echo "cqlsh: Cassandra is unavailable to initialize - will retry later"
  sleep 2
done &

exec /docker-entrypoint.sh "$@"

@veselinnguyen
Copy link

Why the issue has been closed? Has someone found already a solution?

@fogrizovic
Copy link

fogrizovic commented Aug 15, 2018

Hi,
I have succesfully tried the approach from @veysiertekin .

docker-compose.yml

cassandra:
    image: cassandra:2.1.20
    volumes:
      - "./cassandra-init.sh:/cassandra-init.sh"
    command: "sh /cassandra-init.sh"
    healthcheck:
      test: ["CMD-SHELL", "[ $$(nodetool statusgossip) = running ]"]

cassandra-init.sh

CQL="DROP keyspace name;
CREATE KEYSPACE name WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
CREATE TABLE name.blabla (search_hash text, PRIMARY KEY (search_hash));"

until echo $CQL | cqlsh; do
  echo "cqlsh: Cassandra is unavailable to initialize - will retry later"
  sleep 2
done &

exec /docker-entrypoint.sh "$@"

@edugmes
Copy link

edugmes commented Dec 15, 2021

Hi, I have succesfully tried the approach from @veysiertekin .

docker-compose.yml

cassandra:
    image: cassandra:2.1.20
    volumes:
      - "./cassandra-init.sh:/cassandra-init.sh"
    command: "sh /cassandra-init.sh"
    healthcheck:
      test: ["CMD-SHELL", "[ $$(nodetool statusgossip) = running ]"]

cassandra-init.sh

CQL="DROP keyspace name;
CREATE KEYSPACE name WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
CREATE TABLE name.blabla (search_hash text, PRIMARY KEY (search_hash));"

until echo $CQL | cqlsh; do
  echo "cqlsh: Cassandra is unavailable to initialize - will retry later"
  sleep 2
done &

exec /docker-entrypoint.sh "$@"

Thanks for your script! I got it working by adding a if not exists on the create keyspace command. Without that the loop would run indefinately, even after the keyspace was created, as the command would return false to until. The version I used:

cas_query="create keyspace if not exists posts_k with replication = {'class': 'SimpleStrategy', 'replication_factor': 2};"

until echo $cas_query | cqlsh
do
	now=$(date +%T)
	echo "[$now INIT CQLSH]: Node still unavailable, will retry another time"
	sleep 2
done &

exec /usr/local/bin/docker-entrypoint.sh "$@"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests