USB re-connect fails from container

The USB is recognised at startup. But when un-plugged and re-plugged, then the USB is no longer recognised.

Whenever I run the my_service locally, then USB unplug / re-plug works perfectly fine. (I am using the usb-detection library as well as the Serialport.io library. And it can do port recognition after un-plug / re-plug just fine.

It is only inside its own docker container under balenaOS that the USB can never re-connect. Why is that ?

I followed all hints from this earlier post, but without success.

My docker-compose.yml looks like this:

version: "2.1"
services:
    my_service:
        build: my_service_folder
        restart: always
        privileged: true
    my_main_application:
        build: .
        shm_size: "1gb"
        labels:
            io.balena.features.dbus: "1"
        restart: always
        privileged: true
        environment:
            - DISPLAY=:0
            - CURSOR=false
            - PULSE_SERVER=tcp:audio:4317
            - PULSE_SINK=alsa_output.hda-intel.hdmi-stereo
            - DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket

I had also tried with the following change - but without success…

    my_service:
        build: my_service_folder
        restart: always
        privileged: true
        environment:
            - UDEV=1
        devices:
            - '/dev:/dev'

Inside the Dockerfile.template of my_service, I placed:

 ENV UDEV=1

But unfortunately, the USB re-plug does not work.

What am I missing ??

I am using an AMD64

FROM balenalib/%%BALENA_MACHINE_NAME%%-node:16

Here is the entire Dockerfile.template of my_service container:

FROM balenalib/%%BALENA_MACHINE_NAME%%-node:16

# installing preconditions to build usb-detection from source
RUN sudo apt-get update -y
RUN sudo apt-get install -y python3
RUN sudo apt-get install -y build-essential
RUN sudo apt-get install -y libudev-dev

# Defines our working directory in container
WORKDIR /usr/src/app

# Copies the package.json first for better cache on later pushes
COPY package.json yarn.lock ./

# This will copy all files in our root to the working directory in the container
COPY . ./

RUN JOBS=MAX yarn install --frozen-lockfile

# Build dist
RUN yarn run build

# Enable udevd so that plugged dynamic hardware devices show up in our container.
ENV UDEV=1

# Increase to 2 GB
RUN export NODE_OPTIONS="--max-old-space-size=2048"

# server.js will run when container starts up on the device
CMD ["yarn", "start"]

At first glance this looks all correct. Does the lsusb command show you a change in the USB devices after hot-plugging your USB stick?

If lsusb can show the hot-plugged USB device, then the actual reconnection works fine.
In that case the problem might lie with the library you are using to monitor USB devices - maybe similar to the issue I’m having with libudev for monitoring devices: libudev cannot monitor udev events inside container

1 Like