ogr
ogr

Reputation: 690

Docker: using a bind mount locally with swarm

Docker newcomer here.

I have a simple image of a django website with a volume defined for the app directory. I can bind this volume to the actual folder where I do the development with this command :

docker container run --rm -p 8000:8000 --mount type=bind,src=$(pwd)/wordcount-project,target=/usr/src/app/wordcount-project wordcount-django

This works fairly well. Now I tried to push that simple example in a swarm. Note that I have set up a local registry for the image to be available. So to start my service I'd do :

docker service create -p 8000:8000 --mount type=bind,source=$(pwd)/wordcount-project,target=/usr/src/app/wordcount-project 127.0.0.1:5000/wordcount-django

It will work after some tries but only because it run on the local node (where the actual folder is) and not a remote node (where there is no wordcount-project folder).

Any idea how to solve this so that this folder can be accessible to all node and yet, still be accessible locally for development ?

Thanks !

Upvotes: 4

Views: 5414

Answers (1)

Marc ABOUCHACRA
Marc ABOUCHACRA

Reputation: 3463

Using bind-mount in docker swarn is not recommended, as you can read in the doc. In particular :

Important: Bind mounts can be useful but they can also cause problems. In most cases, it is recommended that you architect your application such that mounting paths from the host is unnecessary.

However, if you still want to use bind-mount, then you have two possibility :

  1. Make sure your folder exists on all the nodes. The main problem here is that you'll have to update it everytime on every node.
  2. Use a shared filesystem (such as sshfs for example) and mount it on a directory on each node. However, now that you have a shared filesystem, then you can just use a docker data volume and change the driver.

You can find some documentation on changing the volume data driver here

Upvotes: 3

Related Questions