Manually sharing directory as docker volume mounting point on OSX

喜欢而已 提交于 2019-12-12 02:17:37

问题


According to this tutorial from docker website, When you start a container it automatically shares your /Users/username.

My current problem :

But in my school my $HOME isn't in /Users, and when I try to directly mount volumes on my container with something like docker run -d -P -v $HOME/site:/usr/share/nginx/html --name mysite nginx (like the tutorial from the link), I can't change files through the mounted volume. So I guess my $HOME isn't automatically shared.

But if I run docker instpect mysite I get :

"HostConfig": {
    "Binds": [
        "/nfs/zfs-student-3/users/vmonteco/site:/usr/share/nginx/html"
    ],

and

"Mounts": [
    {
        "Source": "/nfs/zfs-student-3/users/vmonteco/site",
        "Destination": "/usr/share/nginx/html",
        "Mode": "",
        "RW": true,
        "Propagation": "rprivate"
    } 

My question is simply this :

How could I manually share directories with the VM hosting docker (virtualbox here) to use it as volume mounting points?

This is how I did it with Boot2docker before it was deprecated :

With boot2docker I could have run something like :

SHAREDIR=$HOME/share
# creating share folder.
mkdir $SHAREDIR

# mount shared folder while relaunching boot2docker :
boot2docker --vbox-share="$SHAREDIR=share" up
echo "Creating and mounting \"share\" directory in boot2docker VM."
boot2docker ssh "sudo mkdir /share; sudo mount -t vboxsf share /share"

# running the container :
docker run  -ti --rm -v /share:/share -v /Users:/Users base/archlinux sh

And it worked fine.

But now boot2docker is deprecated and I didn't find how to do this with the VM I have to use now (it uses virtualbox).

If I understand well, it should work the same way as boot2docker worked :

                       1                                                  2
[directory from OSX] <===> [VM directory (the VM replaces Boot2docker)] <===> [container directory]

If I still understand well the only that doesn't work now is the point [1], this was handled by the middle part of the little script I just shared with boot2docker.

But how could I do now?


回答1:


See this answer: https://stackoverflow.com/a/32030385/2434234

In summary: Starting in docker 1.8 you can add directories through the vbox cli.

VBoxManage sharedfolder add <machine name/id> --name <mount_name> --hostpath <host_dir> --automount



回答2:


I am a beginner in docker and i want to learn more about this amazing tool to put in production at the company i work for, however, i am trying to have productivity while i am developing my web systems. In this way, i want to share my web app files stored in an OSX local folder to my containers running in NGINX to be easy to test at the same time, without loss of time. Then i found a solution in the internet that i will detail below to you.

First, open the Virtual Box App, click on the docker virtual machine and select the option Settings.

Second, go to the Shared Folders Tab, select the Users item under the Machine Folders section or you can add a new share if it dont exists yet. Write /Users in the Folder Path (path of your OSX local folder), Users in the Folder Name and check if the Auto-Mount and Make Permanent options are both checked.

Confirm the boxes and close the Virtual Box app window.

It is important, reboot your docker virtual machine.

You can use the command below to restart:

docker-machine restart

Finally, you only need to start your container with the command below:

docker run -it -v Local-OSX-Path:Container-Path container-name /bin/bash

Example:

docker run -it -v /Users/username/Documents/Workspaces/www:/var/www/html -p 8080:80 debian-nginx:1.0 /bin/bash

When you access your container is possible to see the files you have in your local OSX directories easily.

I found in another question an another solution - open source project called docker-osx-dev - using Boot2Docker + rsync to do the same thing, but more fast... Feel free to try this one too.

I hope these informations will be enough to solve your problems.

Good Luck!



来源:https://stackoverflow.com/questions/35445690/manually-sharing-directory-as-docker-volume-mounting-point-on-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!