Meraj Rasool
Meraj Rasool

Reputation: 751

Running multiple instances with multiple volumes via docker-compose

I want to be able to run 3 instances of Cassandra via docker with volumes mapped on host machine.

I tried to use it via this docker-compose.yml by running docker-compose up --scale cassandra=3 but it is not starting them correctly and showing errors as I am mapping one volume to all instances.

version: "2"
services:
  cassandra:
      image: cassandra

      volumes:
        - /opt/se/docker-volumes/cassandra:/var/lib/cassandra

Is it possible to map multiple volumes for multiple instances within docker-compose.yml file?

I tried to run 3 instances without defining volumes and it is working fine.

Upvotes: 2

Views: 2263

Answers (2)

yamenk
yamenk

Reputation: 51816

The problem is due to that when the different instances are starting, they are writing data to the same volume. This is causing many race conditions, and is corrupting the /var/lib/cassandra directory for each instance.

To solve this issue, you need to map each instance to a separate volume. You can achieve that by removing the volume mapping inside the compose file, and leave it to docker to create the data volumes, since /var/lib/cassandra is declared as a VOLUME inside the Dockerfile of cassandra.

Alternatively, you can manually declare three cassandra services and map each of them to a separate volume.

Upvotes: 2

GSUgambit
GSUgambit

Reputation: 4889

Is there a reason why you do not want to do it like this?

version: "2"
 services:
   cassandra1:
     image: cassandra
     volumes:
       - /volume1:/var/lib/cassandra
   cassandra2:
     image: cassandra
     volumes:
       - /volume2:/var/lib/cassandra
   cassandra3:
     image: cassandra
     volumes:
       - /volume3:/var/lib/cassandra

Upvotes: 1

Related Questions