Jumpa
Jumpa

Reputation: 4419

Docker Named Volumes

What's the right way to mix named volumes with and without local host path in docker compose v3? This way I'm getting YML error:

volumes:
  /mnt/volume-part1:/volume
  conf:
  vhost:
  html:
  certs:

Then I'd like to refer to volume inside containers...

Upvotes: 0

Views: 1651

Answers (1)

yamenk
yamenk

Reputation: 51906

For named volumes, you need to declare the volume name under the dedicated volumes section in the compose file. For a mount, you don't declare it in that section: Consider the following compose file:

version: "3"

services:
  db:
    image: db
    volumes:
      - data-volume:/var/lib/db
      - /mnt/volume-part1:/volume

volumes:
  data-volume:

As you can see the named volume data-volume needes to be declared in the volumes section before being assiged to the container. Whereas the directory mount is directly mounted onto the container.

UPDATE

If you don't want to replicate the machine path on all the container, you can use a clever trick to specify where exactly the named volume will be created as such:

version: "3"

services:
  db:
    image: db
    volumes:
      - data-volume:/var/lib/db
      - volume-part1:/volume

volumes:
  data-volume:
  volume-part1:
   driver_opts:
     type: none
     device: /mnt/volume-part1
     o: bind

As you can see above, we have created a named volume volume-part1 and specified where this volume will be backuped on the host machine.

Upvotes: 5

Related Questions