Docker Community Forums

Share and learn in the Docker community.

Multiple volumes for docker service create

Does anyone know how to create a new docker service and mount to different, existing, volumes based on the node.

For instance, I want to create a database cluster and link it to mounted volumes on two different nodes. I’ve created the volumes and everything, but I don’t know how to create the service in such a way that it will deploy the containers differently based on which node it deploys them to. Specifically it should mount them to the appropriate volume based on which node it deploys the containers to.

Is anyone aware of how to do this?

Can you elaborate further regarding the use case behind your question?

I am affaid that dynamic assignment of named volumes, depending on the node won’t be possible.

Usualy people are aiming for persistant state accessible from different nodes. Most easiest way to archive this is to directly declare named volumes using a CSI compliant docker volume plugin inside your docker-compose,yml. Though, the question is which one to use :wink:

Due to missing features, reliability and performance, I would strongly advice against using NFS or CIFS network shares as a target for your docker volumes. Even outside the container world is NOT recommended to use those type of network shares to store your database data on them. Moving the context inside a container won’t make things better. On short term, you might feel like they work though, on the long run you will encounter problems.

Specifically, I want a database running on two containers. Lets call them container a and b. I would like the containers running in two separate nodes, lets call them 1 and 2. So in node 1 I’d like to run container a on one volume and in container b I’d like to run a container on another volume. I don’t want these volumes to have anything to do with one another, they should have completely separate memory.

So when I do my docker service create I want to tell docker: hey, if you deploy the container on node 1 use volume-x, but if you deploy the container on node 2, use volume-y. How do you do this?

How about adding labels to your nodes and using deployment constraints to restrict where a specific service is executed?

Adding a label to a node:

$ docker node update --label-add container1=true node1

Creating the service with a deployment constraint to run on a node having a specific label

docker service create \
  --constraint 'node.labels.container1 == true' \
  image:tag

This is a solution, though what I wrote in my previous post is the better option. Use persistance that is accessible from both nodes and let the scheduler take care where the container is started. In case one of your nodes is down (assumed a manager node is still running somewhere) the container would be scheduled on the other node as well.