Docker-Compose and automatic mounts

By | 1 Feb 2025

If you want your Docker container to use data from a network share, people tend to mount the share to a directory outside of Docker and then point the Docker volume to that mounted path. However, Docker-Compose also supports mounting volumes and brings some advantages. E.g. for a SMB/CIFS share, the relevant parts in your compose file could look like this:

services:
  my-service:
    ...
    volumes:
      - my-smb-share:/data:rw

volumes:
  my-smb-share:
    driver_opts:
      type: "smb3"
      device: "//mynas/share"
      o: "rw,vers=3.1.1,addr=192.168.1.20,username=mbirth,password=supersecret,cache=loose,iocharset=utf8,noperm,hard"Code language: PHP (php)

For type you can use anything you have a mount.<type> tool available, e.g. on my Raspberry this would be:

$ ls /usr/sbin/mount.*
/usr/sbin/mount.cifs*  /usr/sbin/mount.fuse3*       /usr/sbin/mount.nilfs2*  /usr/sbin/mount.ntfs-3g@  /usr/sbin/mount.ubifs*
/usr/sbin/mount.fuse@  /usr/sbin/mount.lowntfs-3g@  /usr/sbin/mount.ntfs@    /usr/sbin/mount.smb3@Code language: JavaScript (javascript)

And the o parameter is everything you would put as options to the mount command (e.g. in the 4th column in /etc/fstab or after the -o parameter). In the case of smb3, you can run mount.smb3 --help to see a list of available options.

Doing it this way, Docker will make sure the share is mounted before running the container (and give an error if it can’t mount it). Also, if you copy the compose file to a different host, it’ll just work if the share is reachable from that new location, too.

Likes

Reposts

Leave a Reply

Your email address will not be published. Required fields are marked *

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)