Docker stack shared volume

Hi all,

I am new to Docker and currently stuck trying deploy a stack with apache and mysql using persistant storage for the database.

I understand how to do this as single container (non-swarm), but how does this work if running as a swarm with replicas of mysql service?

I have been trying to get this to work by mounting a samba share as the volume for mysql in a compose file but have failed to get it working. Samba share is mounted and accessible by all nodes in the swarm to /samba-vol

Any suggestions or best practice of how to properly set this up?

This is my docker-compose.yml:
(Apologies for the formatting, all my indentation for some reason was removed from my post)

version: β€˜3.7’
services:
mariadb:
image: mariadb:latest
networks:
- myNetwork
secrets:
- db_root_password
ports:
- β€œ3306:3306”
deploy:
replicas: 3
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
MYSQL_DATABASE: mydatabase
volumes:
- samba-vol:/var/lib/mysql
- samba-vol/sql-scripts:/docker-entrypoint-initdb.d
apache2:
image: apache:latest
args:
- PHP_SOCKET=php:9000
volumes:
- samba-vol/html:/var/www/html
ports:
- 80:80
- 443:443
secrets:
db_root_password:
file: ./secret
networks:
myNetwork:
volumes:
samba-vol:

Does this even work?

  • samba-vol/sql-scripts:/docker-entrypoint-initdb.d

From my experience this will result in a mount-bind which is something you do not want for swarm. You can not attach a path to a named volume on the left hand side of a volume mapping.

Your compose file contains a named volume with default parameters.
As such the volume will be created under /var/lib/docker/volumes/.

You should define and configure it as a cifs type:

volumes:
  test-smb:
    driver_opts:
      type: cifs 
      o: username=myuser,password=mypass,vers=2.0
      device: //192.168.x.x/volumes/mysql

If the volume does not exists on a node, it will be created when the container is scheduled the first time on the node. Though, if the volume already exists, it will NOT get updated with the new settings. You will need to remove it before.

3 Likes

Thanks I will give that a go. Much appreciated!! :slight_smile:

Thanks β€œmeyay”, was just coming back to confirm that your solution worked for me. :slight_smile: