Docker Community Forums

Share and learn in the Docker community.

Not able to mount /usr/local/var from host using Docker compose on mac

I have a folder at /usr/local/var/postgresql@13. This is the derfault path for the datafile when installing Postgres using homebrew.

I’d now like to mount this using the volume command as show below

docker-compose.yml

version: "3.9"
services:
  db:
    image: postgres
    ports:
      - 5432:5432
    volumes:
      - /usr/local/var/:/var/lib/postgresql/data

All other files in the local folder are possible to mount but the var folder. As soon as I try to mount htat Docker tells me

Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /usr/local/var/

I’ve looked at symlinks and permissions without finding anything.

What am I missing?

Which version of mac? And which architecture? Intel or ARM (probably does not mattter)?
How did you “looked at symlinks”? I have tried to reproduce the error without succes on MacOs Bug Sur 11.6 (ARM). I installed postgresql using Homebrew and my folder is not under /usr/local/var but /opt/homebrew/var/postgresql. I could not mount the folder because this is not enabled in Docker Desktop but my error message is different:

The path /opt/homebrew/var/var/postgres is not shared from the host and is not known to Docker.
You can configure shared paths from Docker → Preferences… → Resources → File Sharing.
See Docker Desktop for Mac user manual | Docker Documentation for more info.
ERRO[0000] error waiting for container: context canceled

Thanks for the reply!

To test the symlink I used the readlink tool, and as I understand it just tells me the folder is the actual folder …

Maybe I’ve misunderstood the data folder somehow?

I’m on Intel and Big Sur. May I should just try and reinstall Postgres?

Have a running Postgres service on the host at the same time as I mount the file to the container and the service there couldn’t cause an issue?

Also a bit curious on how to solve the problem you ended up in - how do I get Docker to recognize/be aware of the folder?

Yes, it shows nothing if the file is not a link.

I don’t think that would solve anything but you can try if nothing else helps.

I don’t think so, but if you mounted the same folder inside a container as you used on the host that is definitely a bad idea and can cause problems so don’t do that.

1 Like

I didn’t :slight_smile: I went to the settings to browse that folder and I could not go to /opt. I had a similar issue before without Docker desktop so I created a symlink from my home folder.

I agree with @rimelek that this should not cause the error you’re seeing, but still should not be done: having two PostgreSQL services manage the same data surely doesn’t feel right, if that’s what you’re doing. But again: not the cause of the Docker error, I agree.

Any chance you’re using sudo rights to even see /usr/local/var/?

1 Like

Thanks to both of you!

But maybe I was on the wrong track altogether … Maybe I just should have a namned volume and have the container data files with its own database persisted there. Instead of trying to use the same file as my database running in the host …

1 Like

Or use host.docker.internal:5432 in the container that needs to access your database on the host.

Oh! Didn’t know I could do that :hushed:

I have some new clue. This is similar to another issue in another topic so I was confused and started to answer there referring partailly this issue.

I tried to mount /usr/local/var into the container again but I noticed the folder inside the container was empty. When I saved something into that folder it did not appear on the host. I stopped and deleted the container, deleted each volume and tried again, and the saved file was still there inside the container. After that I mounted the root file system and realised the saved file is on the virtual machine’s filesystem. So I did not have any error message because it could create a folder inside the VM.

When I mounted /opt/homebrew/data/postgres I got an error message. I am not sure what is the difference since none of these folders are enabled to mount in Docker Desktop settings.

However, I could finally allow to mount /opt/homebrew. I had to press Command + SHIFT + . in the file browser window after I selected “Maintosh HD” from the drop down list. The same solution worked for /usr/local/var but only if I had access to it without being an administrator.

It seems it was a good question after all which inspired me to test it.

1 Like