Can named volume in docker compose file create a folder if it does not exist?

Normally I would just give the abs path of the folder in the volume section in the container, but now I’m trying to learn how to make named volumes as my docker compose file needs to share the same volumes sometimes.

My question is: can we create a named volume that bind mount to a folder that has not exist yet?

Named volumes and bind mounts are different things

Bind mount will mount a volume from the host machine’s defined path

A named volume will create a volume managed by docker. It is not bound into a host path and will not just create a folder out of thin air - there is no path defined for that, the named volumes are stored where Docker Daemon stores its files

Both mount types can be used by multiple containers

@tamashika he named volume part is correct for default named volumes (as in non-parameterized).

It is possible to parameterize a named volume, so that it binds a specific folder:

volumes:
  my-data:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /opt/arbitrary/path/data

Note: bind propagation on named volumes backed by a bind can not be configured the same way, like it can be configured for binds. If you have no idea what bind propagation is, then it’s most likely irrelevant for you.

Update: I forget to address @tamashika’s question: the path used in device: must exist in order for the volume to work.

1 Like

Thank you @deanayalon and @meyay.
I think that answer my question.