How to pass a /dev/disk device on macOS into Linux Docker?

7,025

Solution 1

Docker was built for linux, it runs on Mac through HyperkIT, which is a lightweight hypervisor. This means Docker only sees devices connected to the hyperkit hypervisor, rather than the devices connected to the Mac.

Unfortunately, Hyperkit has issues with USB device passthrough from your MacOS to the hypervisor (read more here) so the --device command won't be much use to you here. You have three options,

Option 1: Mount the ext4 drive to your Mac (not recommended)

Whilst your Mac doesn't natively support ext4, there are ways around this. I have installed ext4fuse on your Mac by following this tutorial. When you have mounted your device, add it to your ubuntu container with the -v flag,

docker run -v <Your_volume>:<dest_volume> -it ubuntu bash

Making sure that the directory of your volume has been added to the file sharing folders in Docker.

Option 2: create a docker-machine using the virtualbox driver and add the device to the virtualbox vm (still not recommended)

Follow this great tutorial here which goes through installing a docker machine with a virtualbox driver and mounting the USB stick.

Option 3: Run Docker on linux (recommended!)

I wouldn't recommend using Docker for Mac. It was built for linux and if you don't switch to linux you'll waste your time troubleshooting issues like these for no real reason.

I've got a mac, but run a few ubuntu cores in virtualbox and then install docker on top of them, that way I can play around with docker starm too (which you can't with docker Mac unless you use docker-machine!

Solution 2

As per @pgayvallet comment on 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.

Share:
7,025

Related videos on Youtube

Brett Veenstra
Author by

Brett Veenstra

Updated on September 18, 2022

Comments

  • Brett Veenstra
    Brett Veenstra over 1 year

    I've connected external HDD via USD on macOS, and I'm running Ubuntu's Docker container as:

    docker run -it --device=/dev/disk3 --privileged ubuntu bash
    

    but the device is not present in the container:

    # ls /dev/disk3
    ls: cannot access '/dev/disk3': No such file or directory
    # fdisk -l
    Disk /dev/sda: 59.6 GiB, 63999836160 bytes, 124999680 sectors
    Device     Boot Start       End   Sectors  Size Id Type
    /dev/sda1  *     2048 124999679 124997632 59.6G 83 Linux
    

    Here is another attempt:

    $ docker run -it --device=/dev/disk3:/dev/sdb --privileged ubuntu bash
    # ls /dev/sdb*
    ls: cannot access '/dev/sdb*': No such file or directory
    # fdisk -l
    Disk /dev/sda: 59.6 GiB, 63999836160 bytes, 124999680 sectors
    Device     Boot Start       End   Sectors  Size Id Type
    /dev/sda1  *     2048 124999679 124997632 59.6G 83 Linux
    

    Here is the device which I'm trying to add to the container (as reported on macOS):

    $ diskutil list /dev/disk3
    /dev/disk3 (external, physical):
       #:                       TYPE NAME                    SIZE       IDENTIFIER
       0:     FDisk_partition_scheme                        *1.0 TB     disk3
       1:                      Linux                         1.0 TB     disk3s1
    
    $ sudo fdisk /dev/disk3
    Disk: /dev/disk3    geometry: 121601/255/63 [1953525167 sectors]
    Signature: 0xAA55
             Starting       Ending
     #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
    ------------------------------------------------------------------------
     1: 83 1023 254  63 - 1023 254  63 [         2 - 1953525165] Linux files*
     2: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
     3: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
     4: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
    

    Similar issues are posted here:

    However, I cannot mount it on macOS host, as the partition is formatted to Linux file system (ext4) which is not supported. That's why I'm trying to pass through the device to the Linux Docker container, but the device isn't shown.

    What am I missing?

  • Kubuntuer82
    Kubuntuer82 about 4 years
    Why is option 2 not recommended? what is the difference, in terms of safety, between that solution and using virtualbox directly?
  • Alexey Vazhnov
    Alexey Vazhnov about 3 years
  • SanthoshP2888
    SanthoshP2888 over 2 years
    Good answer excepting the suggestion to switch to Linux.