title: How to create a Dockerfile for cassandra (or any database) that includes a schema?
body: <p>I would like to create a dockerfile that builds a Cassandra image with a keyspace and schema already there when the image starts.</p>
  
  <p>In general, how do you create a Dockerfile that will build an image that includes some step(s) that can't really be done until the container is running, at least the first time?</p>
  
  <p>Right now, I have two steps:   build the cassandra image from an existing cassandra Dockerfile that maps a volume with the CQL schema files into a temporary directory, and then run docker exec with cqlsh to import the schema after the image has been started as a container.</p>
  
  <p>But that doesn't create an image with the schema - just a container.  That container could be saved as an image, but that's cumbersome.</p>
  
  <pre><code>    docker run --name $CASSANDRA_NAME -d \
          -h $CASSANDRA_NAME \
          -v $CASSANDRA_DATA_DIR:/data \
          -v $CASSANDRA_DIR/target:/tmp/schema \
          tobert/cassandra:2.1.7
  </code></pre>
  
  <p>then</p>
  
  <pre><code>docker exec $CASSANDRA_NAME cqlsh  -f /tmp/schema/create_keyspace.cql
  docker exec $CASSANDRA_NAME cqlsh  -f /tmp/schema/schema01.cql
  # etc
  </code></pre>
  
  <p>This works, but it makes it impossible to use with tools like Docker compose since linked containers/services will start up too and expect the schema to be in place.</p>
  
  <p>I saw one attempt where the cassandra process as attempted to be started in the background in the Dockerfile during build, then cqlsh run, but I don't think that worked too well.</p>