geoidesic
geoidesic

Reputation: 5063

How to access shared volumes on Docker for Mac

I've reviewed the documentation here:

https://docs.docker.com/docker-for-mac/install/#install-and-run-docker-for-mac

It doesn't say anything about boot2docker, although some other questions along these lines talk about this:

Mount volume to Docker image on OSX

So the question is – the Docker for Mac application provides File Sharing via Preferences -> File Sharing; how does one make use of these shared folders from the docker image (for example if one ssh's into the docker image)? When I say how, I don't mean "what are the use-cases", I mean "please show me an example of how to access a shared folder from the command line of the running container".

Ideally I'm trying to create a similar scenario to Vagrant's synched folders whereby I can edit files on my Host env, independently of the Docker Image but these are updated automatically to the Docker image on save.

UPDATE:

To be clear, the reason for asking this question is because I couldn't get the -v docker command to work. E.g.

docker run -v /Users/geoidesic/Documents/projects/arc/mysite/djangocms_demo:/home/djangocms/djangocms/djangocms_demo -d -p 8001:8000 --name test_shared_volumes bluszcz/djangocms

With the above command the container immediately stops, so if I run docker ps the list of running containers is empty.

However, if I run the container without the -v command, then it stays running as expected:

docker run -d -p 8001:8000 --name test_shared_volumes bluszcz/djangocms

Upvotes: 2

Views: 5987

Answers (1)

Yuankun
Yuankun

Reputation: 7813

Updated:

Well, if you want to share file/directory between host and container, you're gonna use Docker's bind-mount.

For example, if I want to share my host's /etc/resolv.conf to my container, I do the following:

docker run -v /etc/resolv.conf:/etc/resolv.conf <IMAGE>

In which the -v ... part tells the container to reuse host's /etc/resolve.conf. And whenever I edit this file, the changes will be immediately visible to the container.

In Linux, you can use this way to share almost any of your host files to containers. Unfortunately, this is not the case for Mac. As I mentioned in my old answer, by default you can only share /Users/, /Volumes/, /private/, and /tmp directly.

On my Mac, saying, I want to share the /data directory to a container. I run below command:

docker run -it --rm -v /data:/data busybox sh

Then it pops up an unhappy error:

docker: Error response from daemon: Mounts denied:
The path /data
is not shared from OS X and is not known to Docker.
You can configure shared paths from Docker -> Preferences... -> File Sharing.
See https://docs.docker.com/docker-for-mac/osxfs/#namespaces for more info.

So you see, this is where File Sharing comes up.

Then comes my answers to your questions:

  1. File Sharing does not provide you a ready-to-use way to do the sharing as you have experienced in Vagrant;
  2. To share file/folder between host and container, use Dockers bind-mount.

Hope that helps.


Old answer:

File Sharing is used by Docker's bind-mount feature. By default, you can bind-mount files in /Users/, /Volumes/, /private/, and /tmp directly. For other paths, you need to add them to Preferences -> File Sharing first.

Use cases for bind-mount:

  1. Persisting data generated by the running container, so that you can backup or migrate data.
  2. Sharing data amount multiple running containers.
  3. Share host configuration files to containers.
  4. Share source code between host and containers, to make debugging easier.

Note: For cases #1 and #2, consider using volumes instead of bind-mount.

Upvotes: 4

Related Questions