Search code examples
dockerudev

How can I dynamically get symlinks to devices created by udev running on Host in a docker container


I want to dynamically get symlinks to devices created by udev running on Host in a docker container

I was able to bind the symlink to the container but it's not dynamically recreated if the device is removed (e.g: usb is disconnected)

Udev rules example:

SUBSYSTEM=="tty", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="140c", MODE="0666", SYMLINK+="my_dir/gsm-modem0"

docker run example:

sudo docker run -v /dev/my_dir/gsm-modem0:/dev/my_dir/gsm-modem0 my_image my_script.sh

Solution

  • Answer:

    Udev rule should symlink to a new directory:

    SUBSYSTEM=="tty", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="140c", MODE="0666", SYMLINK+="my_dir/gsm-modem0"
    
    

    Running docker must contain --privileged:

    sudo docker run --privileged -v /dev/my_dir:/dev/my_dir my_image my_script.sh
    
    

    and my_script.sh should start by creating a new file in the created symlink directory:

    mkdir -p /dev/my_dir
    touch /dev/my_dir/keep
    

    Explanation:

    For some reason udev may delete the link directory if the directory is empty, and since usually /dev is a tmpfs creating new file won't survive restart. Touching a file on every run will keep the link containing directory on host and if a new link is created it will appear on the container