# USB re-connect fails from container

**URL:** <https://forums.balena.io/t/usb-re-connect-fails-from-container/363284>\
**Category:** balenaOS\
**Created:** [September 25, 2022, 8:54pm UTC](https://forums.balena.io/t/usb-re-connect-fails-from-container/363284 "2022-09-25T20:54:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skuenstler](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@skuenstler](https://forums.balena.io/u/skuenstler)\
**Post date:** [September 25, 2022, 8:54pm UTC](https://forums.balena.io/t/usb-re-connect-fails-from-container/363284/1 "2022-09-25T20:54:57Z")

</div>

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](https://github.com/MadLittleMods/node-usb-detection) library as well as the [Serialport.io](https://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](https://forums.balena.io/t/docker-container-cannot-access-dynamically-plugged-usb-devices/4277), but without success.

My docker-compose.yml looks like this:

```auto
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…

```auto
    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:

```auto
 ENV UDEV=1

```

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

What am I missing ??

I am using an AMD64

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

```

Here is the entire `Dockerfile.template` of `my_service` container:

```auto
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"]

```

---

<div class="post-metadata">

**Author:** ![rapha](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/rapha/32/1239_2.png) [@rapha](https://forums.balena.io/u/rapha)\
**Post date:** [September 28, 2022, 8:20pm UTC](https://forums.balena.io/t/usb-re-connect-fails-from-container/363284/2 "2022-09-28T20:20:28Z")

</div>

At first glance this looks all correct. Does the [`lsusb`](https://command-not-found.com/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](https://forums.balena.io/t/libudev-cannot-monitor-udev-events-inside-container/363638)

---

<div class="post-metadata">

**Author:** ![skuenstler](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@skuenstler](https://forums.balena.io/u/skuenstler)\
**Post date:** [December 26, 2022, 4:28pm UTC](https://forums.balena.io/t/usb-re-connect-fails-from-container/363284/3 "2022-12-26T16:28:18Z")

</div>

Sorry for my long response-time and thank you very much for your helpful hint.

The cmd `lsusb` in the service-container terminal indeed always brought me the correct USB-devices. And therefore, for a long time, I also suspected the usb-detection library to be the root cause of non-working.

However, it turned out that the library is just fine and that the following brought the solution:

Next to `privileged: true` and `environment: - UDEV=1`, also make sure to set the `network_mode: host` inside the docker-compose.yml file of your service-definition.

From the Balena documentation we learn `network_mode: host` does the following:

```auto
Setting network_mode to host allows the container to share the same network namespace as the host OS.

When this is set, any ports exposed on the container will be exposed locally on the device.

This is necessary for features such as bluetooth.

```

And turns out, not only for bluetooth but also for services that need USB-detection.

---

<div class="post-metadata">

**Author:** ![skuenstler](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@skuenstler](https://forums.balena.io/u/skuenstler)\
**Post date:** [December 26, 2022, 8:12pm UTC](https://forums.balena.io/t/usb-re-connect-fails-from-container/363284/4 "2022-12-26T20:12:09Z")

</div>

Remains the question how to achieve a working `usb-detection` without `network_mode: host` setting on the multicontainer service - especially for those projects that are not able to set their services in the same namespace as host ???

As long as I run the bridge-network mode, then the `privileged: true` and `environment: - UDEV=1` is definitively not enough ! Any idea what to do in order to recognize usb-hotplug events ??? (after months, pretty desperate about this)

---

<div class="post-metadata">

**Author:** ![skuenstler](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@skuenstler](https://forums.balena.io/u/skuenstler)\
**Post date:** [February 1, 2023, 3:33pm UTC](https://forums.balena.io/t/usb-re-connect-fails-from-container/363284/5 "2023-02-01T15:33:08Z")

</div>

The solution [is described here](https://forums.balena.io/t/x11-hotplug-usb/7250/30).

You have to restart the UDEV as [bversluijs](https://forums.balena.io/u/bversluijs) describes in his solution.
