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 · 16 comments

Comments

Projects
None yet
@ghost

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

This comment has been minimized.

rodrigorodrigues commented Jun 20, 2017

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

This comment has been minimized.

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

This comment has been minimized.

ghost commented Aug 5, 2017

Hey guys, hope this helps ;-)

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

@MatteoJoliveau

This comment has been minimized.

MatteoJoliveau commented Sep 20, 2017

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

@Chabane

This comment has been minimized.

Chabane commented Sep 22, 2017

@dschroe not working, the CQL scripts are never executed

@ghost

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

moharnab123saikia commented Mar 30, 2018

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

@tianon

This comment has been minimized.

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 Apr 20, 2018

@sourcekick

This comment has been minimized.

sourcekick 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

This comment has been minimized.

veysiertekin commented Jul 28, 2018

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

This comment has been minimized.

veselinnguyen commented Aug 15, 2018

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

@fogrizovic

This comment has been minimized.

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 "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment