Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to share folders with docker containers on OSX

How to share a folder with a docker container on OSX

Mounting shared folders between OSX and the docker container is tricky due to the intermediate boot2docker VM. You can't use the usual docker -v option as the docker server knows nothing about the OSX filesystem - it can only mount folders from the boot2docker filesystem. Fortunately, you can work around this using SSHFS.

Install sshfs on the boot2docker machine:

$ boot2docker ssh
docker@boot2docker:~$ tce-load -wi sshfs-fuse

and create a folder to mount to:

docker@boot2docker:~$ mkdir ~/osx

Ensure that 'Remote Login' is enabled in 'System Preferences > Sharing' and make a note of your host IP address.

Now run sshfs on the boot2docker VM to mount a folder from your host machine:

docker@boot2docker:~$ sudo sshfs $username@$ipaddress:/Users/$username/ /home/docker/osx/

replacing $username and $ipaddress as appropriate. You'll have to enter your password.

Now you can run a docker container (using the docker client on your host machine) and mount a local folder (using the path to it on the boot2docker VM), eg:

$ docker run -it -v /home/docker/osx/somefolder:/opt/somefolder ubuntu bash

and any updates made within your local folder will be immediately visible within the container.

Largely taken from the comments here: https://github.com/boot2docker/boot2docker/issues/188

@chrismckinnel

This comment has been minimized.

chrismckinnel commented Jun 13, 2014

It's worth noting here that if you're ssh'd into the boot2docker VM as the docker user, after running the sshfs command above if you try and ls -la on the docker home dir to test that your mount worked you won't have access:

docker@boot2docker:~$ ll /home/docker/
ls: /home/docker/osx: Permission denied
total 4
----------    1 docker   staff           29 Jan  1  1970 boot2docker, please format-me

So if you sudo -s you'll get some love:

docker@boot2docker:~$ sudo -s
root@boot2docker:/home/docker# ls -la
total 20
drwxr-sr-x    5 docker   staff          180 Jun 13 10:26 . 
drwxrwxr-x    3 root     staff           60 Jun 13 10:26 ..
-rw-rw-r--    1 docker   staff          307 Jun 13 10:50 .ash_history
-rw-r--r--    1 docker   staff          446 Jun 13 10:26 .ashrc
drwxr-sr-x    3 docker   staff           60 Jun 13 10:26 .local
-rw-r--r--    1 docker   staff          920 Jun 13 10:26 .profile
drwx--S---    2 docker   staff           80 Jan  1  1970 .ssh
----------    1 docker   staff           29 Jan  1  1970 boot2docker, please format-me
drwxrwxr-x    1 10133    10000         2006 Jun 13 10:46 osx

The permissions on the mounted folder are sweet as for mounting it as a volume with docker though.

Party time.

@visigoth

This comment has been minimized.

visigoth commented Aug 20, 2014

for whatever reason, i am not able to ssh back to my host machine. i do have ssh enabled there, and in os x, i'm able to ssh to localhost. trying to ssh back to my host machine's IP address on vboxnet0 leaves ssh in a "connecting" state. telnet 22 also seems to hang in a connecting state. not sure what's going on. any ideas?

@m3nu

This comment has been minimized.

m3nu commented Aug 24, 2014

Works as promised.

@crucialfelix

This comment has been minimized.

crucialfelix commented Nov 17, 2014

note: boot2docker automatically mounts /Users now this solution isn't needed anymore. just go into boot2docker instance and ls /Users

@ghost

This comment has been minimized.

ghost commented Nov 20, 2014

+1 @crucialfelix - thank you!

@scald

This comment has been minimized.

scald commented Dec 10, 2014

Thanks @crucialfelix - I was able to mount quickly like this:

run -it -v /Users:/Users imagename bash

@ofostier

This comment has been minimized.

ofostier commented Jun 5, 2015

Hi all,

I got an
read: Connection reset by peer

when i enter my password any ideas ?

Thx

@WishCow

This comment has been minimized.

WishCow commented Jul 6, 2015

Is the information provided by @crucialfelix still true? We are trying to set this up on a mac, but /Users is definitely not mounted.

@denisinla

This comment has been minimized.

denisinla commented Jul 9, 2015

Make sure you're running the latest boot2docker v1.7.0

@Elijen

This comment has been minimized.

Elijen commented Aug 12, 2015

tce-load -wi sshfs-fuse Produces the following error for me :(

Downloading: sshfs-fuse.tcz
Connecting to repo.tinycorelinux.net (89.22.99.37:80)
wget: server returned error: HTTP/1.1 404 Not Found
md5sum: sshfs-fuse.tcz.md5.txt: No such file or directory
Error on sshfs-fuse.tcz
@igormukhingmailcom

This comment has been minimized.

igormukhingmailcom commented Aug 21, 2015

Has same issue:

docker@default:~$ tce-load -wi sshfs-fuse

Downloading: sshfs-fuse.tcz
Connecting to repo.tinycorelinux.net (89.22.99.37:80)
wget: server returned error: HTTP/1.1 404 Not Found
md5sum: sshfs-fuse.tcz.md5.txt: No such file or directory
Error on sshfs-fuse.tcz

Maybe because I have latest versions:

Boot2Docker version 1.8.1, build master : 7f12e95
Docker version 1.8.1, build d12ea79
@fyddaben

This comment has been minimized.

fyddaben commented Sep 8, 2015

sudo sshfs -o allow_other user@myserver:/home/user/myprojects ~/mount/myprojects

need add allow_other, or noroot user cant have permissio

@fyddaben

This comment has been minimized.

fyddaben commented Sep 8, 2015

but when i restart boot2docker ,
sshfs : command not found
why?

@thalesfsp

This comment has been minimized.

thalesfsp commented Dec 29, 2015

@codeinthehole it's still necessary or the new docker-machine provide it? I'm trying to get hot-reload (NodeJS) with PM2 or Nodemon. Cheers!

@Blizzke

This comment has been minimized.

Blizzke commented Feb 6, 2016

404 for the sshfs-fuse install here as well. Getting a bit irritated by how much work this is, simply to get a folder mounted

@anupvarghese

This comment has been minimized.

anupvarghese commented Jul 16, 2016

With native OSX app, it can be done easily as follows,

To pull a new docker image (optional step)
docker pull ubuntu

Make volume share as below,
docker run -t -i -v $(pwd):/home/shared ubuntu /bin/bash

@ReaddyEddy

This comment has been minimized.

ReaddyEddy commented Sep 14, 2016

Agreed this worked for me
docker run -it -v /Volumes/volname:/volname image-id /bin/bash

@inancgumus

This comment has been minimized.

inancgumus commented May 14, 2017

How about mounting a volume from a different docker-machine to a local docker host? Such as a docker-machine running in digitalocean droplet to my macbook?

@pilgrim2go

This comment has been minimized.

pilgrim2go commented Jun 7, 2017

Thanks @fyddaben @codeinthehole: it works for me

@DanZeuss

This comment has been minimized.

DanZeuss commented Jun 12, 2017

Hi Guys, I did the inverse and worked faster with less steps to get it working:

https://github.com/DanZeuss/DockerOnMacOS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment