简体   繁体   English

如何从docker容器内访问OSX主机上的USB驱动器?

[英]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 (eg, such as AWS or Google Cloud ) packaged inside a docker image. 我有一个应用程序,我最终希望在一个包含在docker镜像中的云计算服务(例如AWSGoogle Cloud )上运行。 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. 应用程序需要在云中运行的原因是它设计用于处理大型数据文件,但在实际部署之前,我想首先在本地笔记本电脑上测试它,使用我所拥有的单个大型数据文件存储(用于测试和开发目的)在外部USB驱动器上。

My development machine is an OSX laptop, and I'm using a recent version of docker: 我的开发机器是OSX笔记本电脑,我使用的是最近版本的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 : OSX已将我的外部USB驱动器设备/dev/disk2s2/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: 还应该注意,驱动器有几个目录和数据在其中可见,至少直接通过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. 我试图遵循这个问题的建议, 该问题描述了当docker在linux主机上运行时如何在docker中安装USB驱动器。 But my local laptop is OSX, not linux, so it doesn't seem to work. 但我的本地笔记本电脑是OSX,而​​不是Linux,所以它似乎不起作用。

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. 根据响应,可以看到docker确实正确启动了一个linux容器,并且它还根据请求在容器内部创建了一个卷/ dev / foo,但是无法通过该位置访问USB驱动器的实际内容 - -s ls -l命令声称那里没有文件或目录。

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. 我在stackoverflow上发现了另一个讨论线程 ,它表明原始USB访问在OSX中的处理方式与在Linux中完全不同,我怀疑这可能是上述两种USB访问尝试都失败的原因。

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? 也就是说,允许docker访问安装在OSX主机而不是linux上的USB设备的正确动作或命令序列是什么?

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 我终于能够通过使用从我的容器内的/ var /媒体访问我的USB驱动器machine-diskutil.sh在warmoverflow的评论,像这样提到脚本

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. 因为我曾尝试在VirtualBox中手动添加/ Volumes / my-usb-drive作为共享文件夹,所以我首先遇到此错误。

Error: The shared folder /Volumes/Seagate already exists on the docker machine, please unmount it first. 错误:docker机器上已存在共享文件夹/Volumes/Seagate ,请先卸载它。

So I removed it manually and re-ran the machine-diskutil.sh mount command without any problems. 所以我手动删除它并重新运行machine-diskutil.sh mount命令没有任何问题。 Great stuff! 好东西!

As per @pgayvallet comment on GitHub : 根据@pgayvallet对GitHub的评论

As the daemon runs inside a VM in Docker Desktop, it is not possible to actually share a mac host device with the container inside the VM , and this will most definitely never be possible. 由于守护程序在Docker Desktop中的VM内运行, 因此无法与VM内的容器实际共享mac主机设备 ,这绝对不可能实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM