How to add ephemeral usb devices to the docker container


Usually you can mount the device by defining that in your docker-compose file or by command line.

But docker container won’t start if the device doesn’t exists at this moment.

For this you can use the follwing workaround.

  1. Connect your device to the host system to get the required information for further processing.
  2. Get the information by typing the follwing command:
    cat /sys/class/tty/ttyUSB0/uevent

    Result:
    MAJOR=188
    MINOR=0
    DEVNAME=ttyUSB0
  3. Add to your docker-compose file the following configuration:
    devices:
    - /dev/bus/usb
    volumes:
    - /run/udev:/run/udev:ro # so that serial number vendor/product ids are known
    device_cgroup_rules:
    - 'c 188:* rmw' # allow creation of /dev/ttyUSBx nodes via mknod (see uevent:MAJOR of the device)
  4. In running container you can now add the device file using the following command, event if the device is not connected at the time:
    mknod /dev/ttyUSB0 c 188 0

Leave a comment

Your email address will not be published.