Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Swarm: How to handle persistent data (e.g. database)

I have 3-node docker swarm mode cluster (1 manager and 2 workers).

Now I want to deploy a mongodb service to the cluster and I would like to store the database on a persistent storage that I've mounted under /mnt/data. So basically I want the service to use my mounted storage inside each containers /db/data.

How do I achieve this?

I made myself familiar with docker service create --mount but

  • I am not sure if I should use a volume or a bind-mount ?
  • if I choose volume how can I tell docker to store this volume on /mnt/data

To couple the mongodb services to a specific node (where the storage is mounted) I added a label to the corresponding node and start the docker service with a constraint.

like image 833
zarathustra Avatar asked Oct 18 '22 12:10

zarathustra


1 Answers

you need to bind mount the volume with mongodb data. follow the below steps on master server

  1. docker volume create -d local --name mongodata
  2. docker service create --network mongo \ --mount type=volume,source=mongodata,target=/data/db \ --constraint 'node.role == manager' \ mongo:3.2

you can also look at setting up mongodb cluster in swarm to have the db running in HA mode. follow the below link https://medium.com/@kalahari/running-a-mongodb-replica-set-on-docker-1-12-swarm-mode-step-by-step-a5f3ba07d06e

like image 121
P Ekambaram Avatar answered Oct 23 '22 09:10

P Ekambaram