Specifying top-level and service-level volumes in compose file

Since docker > =26.0 volumes started to support volume subpaths:
https://docs.docker.com/compose/compose-file/05-services/#long-syntax-5

I am on 26.1.0, but couldn’t make it work with bind backed volumes. The syntax should look like this:

services:
  thisapp_db:
    image: mariadb
    volumes:
      - type: volume
        source: forthisapplication_data
        volume:
          subpath: mariadb/mysql
        target: /var/lib/mysql:Z

Note: I am not sure, if volume subpaths actually work on a bind backed volume, and whether appending the access mode “:Z” actually works with the long syntax. I know it’s supported if type: bind and selinux: Z is used in the volumes bind property…

Update: I tried it now with a named volume backed by an nfsv4 remote share → works like a charm. Though, I still can’t get it working with a named volume backed by a bind.

1 Like