membersound
membersound

Reputation: 86935

How to set absolute path for docker volume in docker-compose?

I have a quite simple docker-compose.yml that creates an initial volume for a mysql container:

version: '3.7'
services:
  db:
    image: mysql:8.0
    volumes:
      - database:/var/lib/mysql
   
volumes:
  database:
    name: mysql-database

Question: how can I tell docker to create the volume on a specific absolute path on first startup, instead of creating it at /var/lib/docker/volumes/mysql-database/?

I would like to set the target path to /mnt/docker/volumes/mysql-database/ for having it on another disk. But only this volume, not all docker volumes!

Is that possible?

Upvotes: 4

Views: 13339

Answers (3)

membersound
membersound

Reputation: 86935

I found a way to create a "named bind mount", having the advantage that docker volume ls will still show the volume, even it is a bindmount:

Create: docker volume create --driver local --opt type=none --opt device=/mnt/docker/volumes/mysql-database --opt o=bind mysql-database

Usage:

volumes:
  database:
    name: mysql-database
    external: true

Upvotes: 3

Tolis Gerodimos
Tolis Gerodimos

Reputation: 4428

With the local volume driver comes the ability to use arbitrary mounts; by using a bind mount you can achieve exactly this.

For setting up a named volume that gets mounted into /mnt/docker/volumes/mysql-database, your docker-compose.yml would look like this:

volumes:
  database:
    driver: local
    driver_opts:
      type: 'none'
      o: 'bind'
      device: '/mnt/docker/volumes/mysql-database'

By using a bind mount you lose in flexibility since it's harder to reuse the same volume. But you don't lose anything in regards of functionality

Upvotes: 3

BertC
BertC

Reputation: 2676

Maybe not use the volumes section since you don't know where docker puts your data.

So, specify the full path. It does work for our installation. Interested to know when this does not work for your situation.

version: '3.7'
services:
  db:
    image: mysql:8.0
    volumes:
      - /mnt/docker/volumes/mysql-database:/var/lib/mysql

Upvotes: 2

Related Questions