Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access a USB drive on a OSX host from inside a docker container?

I have an application that I eventually want to run on a cloud computing service (e.g., such as AWS or Google Cloud) packaged inside a docker image. The reason the application will need to run in the cloud is because it's designed to process large data files, but before I actually deploy, I'd like to test it first on a local laptop, using a single large data file that I've stored (for test and development purposes) on an external USB drive.

My development machine is an OSX laptop, and I'm using a recent version of docker:

stachyra> uname -a
Darwin Andrews-MacBook-Pro-76.local 14.5.0 Darwin Kernel Version 14.5.0: Tue Sep  1 21:23:09 PDT 2015; root:xnu-2782.50.1~1/RELEASE_X86_64 x86_64
stachyra> docker --version
Docker version 1.10.2, build c3959b1

OSX has mounted my external USB drive, device /dev/disk2s2, as /Volumes/MGR DATA:

stachyra> df
Filesystem    512-blocks       Used Available Capacity   iused    ifree %iused  Mounted on
/dev/disk1     974770480  435721376 538537104    45%  54529170 67317138   45%   /
devfs                375        375         0   100%       650        0  100%   /dev
map -hosts             0          0         0   100%         0        0  100%   /net
map auto_home          0          0         0   100%         0        0  100%   /home
/dev/disk2s2  3906291632 3869523640  36767992   100% 483690453  4595999   99%   /Volumes/MGR DATA
/dev/disk3s1      196608     193160      3448    99%     24143      431   98%   /Volumes/VirtualBox
stachyra> diskutil list
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.3 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:          Apple_CoreStorage                         499.4 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                  Apple_HFS Macintosh HD           *499.1 GB   disk1
                                 Logical Volume on disk0s2
                                 DB70B91A-3B57-4C82-A758-C4BDEA4160FD
                                 Unlocked Encrypted
/dev/disk2
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *2.0 TB     disk2
   1:                        EFI EFI                     209.7 MB   disk2s1
   2:                  Apple_HFS MGR DATA                2.0 TB     disk2s2
/dev/disk3
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *100.7 MB   disk3
   1:                  Apple_HFS VirtualBox              100.7 MB   disk3s1

and it should also be noted, the drive has several directories and data which are visible inside it, at least when viewed directly through OSX:

stachyra> ls -l /Volumes/MGR\ DATA
total 0
drwxr-xr-x   6 stachyra  staff  204 Apr 14  2015 1000genomes
drwxr-xr-x   5 stachyra  staff  170 Oct 12 17:41 GIAB
drwxr-xr-x   4 stachyra  staff  136 Apr 28  2015 genome_browser_tracks
drwxr-xr-x  24 stachyra  staff  816 Oct  6 14:00 mitty

I have tried to follow the advice from this question, which describes how to mount a USB drive in docker when docker is running within a linux host. But my local laptop is OSX, not linux, so it doesn't seem to work.

Explicitly, when attempting to follow the advice of the accepted answer, I obtain the following result:

stachyra> docker run -i -t --privileged -v /dev/disk2s2:/dev/foo ubuntu bash
root@8da7b492a707:/# uname -a
Linux 8da7b492a707 4.1.18-boot2docker #1 SMP Sat Feb 20 08:24:27 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
root@8da7b492a707:/# ls -l /dev/foo
total 0
root@8da7b492a707:/# 

Based upon the response, one can see that docker does indeed launch a linux container correctly, and it also creates a volume /dev/foo inside of the container as requested, but the actual contents of the USB drive are not accessible via that location--the ls -l command claims there are no files or directories there.

I also tried the second method described in an alternate response to the same question, and that fails even worse:

stachyra> docker run -i -t --device=/dev/disk2s2 ubuntu bash
docker: Error response from daemon: error gathering device information while adding custom device "/dev/disk2s2": not a device node.
                                                                                                                                stachyra> 

I have found another discussion thread on stackoverflow which suggests that raw USB access is handled quite differently in OSX than in linux, which I suspect is probably the reason why both of the above attempts at USB access are failing.

But, what should I actually do about it? That is to say, what is the correct sequence of actions or commands to allow docker to access a USB device mounted on an OSX host, rather than linux?

like image 853
stachyra Avatar asked Mar 07 '16 21:03

stachyra


1 Answers

I was finally able to access my USB drive from /var/media inside my container by using the machine-diskutil.sh script mentioned in warmoverflow's comment like so

machine-diskutil.sh mount my-machine-name /Volumes/my-usb-drive

and then starting the container like so

docker run -v /Volumes/my-usb-drive:/var/media -it my/image:latest bash

Because I had tried to add /Volumes/my-usb-drive as a shared folder manually in VirtualBox, I first got this error.

Error: The shared folder /Volumes/Seagate already exists on the docker machine, please unmount it first.

So I removed it manually and re-ran the machine-diskutil.sh mount command without any problems. Great stuff!

like image 83
Nick Weisser Avatar answered Sep 27 '22 17:09

Nick Weisser