Top-level volume with absolute path

In a .yml file, is it possible to have a top-level volume with absolute path? Something like:

services:
  foo:
    volumes:
      - bar:/dir_container
volumes:
  bar:
    - /dir_host

So, I don’t want Docker to create a volume bar, but mount the host directory /dir_host as volume bar. Then, bar will be mounted on the foo service under /dir_container.

I know I can do:

services:
  foo:
    volumes:
      - /dir_host:/dir_container

But I would like to have /dir_host specified once and mounted by multiple services (us bar). Also, I would like to avoid volumes_from since it is phased out.

2 Likes
services:
  foo:
    volumes:
      - bar:/dir_container

volumes:
  bar:
    driver: local
    driver_opts:
      device: /dir_host
      o: bind
3 Likes

I can’t seem to find any documentation on the existing driver options for the local driver. What does o stand for? What other options are there for o and what other paramters can be set?

-o are the linux mount options. It is documented, abstractly, on the docker volume creation page.

The options are the options available for the -o param on the mount command.

In this case it seems to be a Bind mount operation.

1 Like

What’s apparently missing (I got an error) is the type attribute:

services:
  foo:
    volumes:
      - bar:/dir_container

volumes:
  bar:
    driver: local
    driver_opts:
      device: /dir_host
      o: bind
      type: local

With this set, it works flawlessly.