Change source path of volume
Asked Answered
M

1

10

This dockerfile works as expected. But the problem is that I am not able to change the source of volume.

version: "3.5"
services:
  mysql:
    environment:
      MYSQL_ROOT_PASSWORD: india3391
    image: shantanuo/mysql:5.7
    ports:
    - mode: ingress
      target: 3306
      published: 3391
      protocol: tcp
    volumes:
    - type: volume
      source: dbvol
      target: /var/lib/mysql
volumes:
  dbvol: {}

It seems to create a directory_name + volume_name folder in default docker installation that looks like this...

"Source": "/var/lib/docker/volumes/hashi1_dbvol/_data",

Is there a way to change this path?


Update:

# cat docker-compose.yml
version: "3.5"
services:
  mysql:
    environment:
      MYSQL_ROOT_PASSWORD: india3391
    image: shantanuo/mysql:5.7
    ports:
    - mode: ingress
      target: 3306
      published: 3391
      protocol: tcp
    volumes:
    - type: volume
      source: dbdata
      target: /var/lib/mysql

volumes:
  dbdata:
    driver: local
    driver_opts:
      type: 'none'
      o: 'bind'
      device: '/srv4/db-data'

This compose file will work only if I first create the mount directory

mkdir -p /srv4/db-data

The volume parameter of docker run command will create this directory on the fly. Why is docker compose not able to create the directory?

Myrica answered 23/6, 2018 at 6:13 Comment(5)
Would https://mcmap.net/q/295628/-how-to-change-the-default-location-for-quot-docker-create-volume-quot-command help? Or using a symlink? (#36015054)Chickenhearted
The answer is there: #36387532 Let me know it this is what yo want.Remy
@Remy I have updated my question. That answer is correct but unlike docker run, I need to first create a directory on host machine. Can this step be avoided?Myrica
I think this feature has been removed on request. for more detail : github.com/docker/compose/issues/2781Jewbaiting
@chintanthakar That page suggest that docker run does not create directory which is not the case. Right? Automatic dictionary creation is a great feature. User should have that option through a parameter.Myrica
P
0

Docker Compose doesn't automatically allow create host level directories automatically for bind mounts for safety reasons.

Docker Compose tries to enforce explicit configuration and it expects the user to create the directories.

There are 2 ways to achieve it.

  1. manually creating the directories upfront before running the docker-compose command
  2. Better approach, using an entrypoint script to handle it

entrypoint.sh

#!/bin/bash
mkdir -p /srv4/db-data
docker-compose up -d
Parade answered 10/9 at 1:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.