Cannot map USB device in docker - Error

I’m trying to migrate from HAOS on my NUC to HA running in a Docker container. I get the container set up fine and managed to use my backup tar.gz file to restore most of the setup I was running on the OS. There is a problem when I edit the docker-compose.yaml to map USB device (sonoff zigbee stick).

I add the line exactly from the documentation (when I run ls -l /dev/serial/by-id it shows ttyUSB0):

    devices:
      # your usb device forwarding to the docker image
      - /dev/ttyUSB0:/dev/ttyUSB0

Then when I save the docker-compose and run docker-compose up -d I get a long winded error message. If I remove the devices section and do the command again I get no errors.

evan@evan-NUC6CAYH:/opt$ docker-compose up -d
portainer is up-to-date
Recreating homeassistant ...

ERROR: for homeassistant  'ContainerConfig'

ERROR: for homeassistant  'ContainerConfig'
Traceback (most recent call last):
  File "docker-compose", line 3, in <module>
  File "compose/cli/main.py", line 81, in main
  File "compose/cli/main.py", line 203, in perform_command
  File "compose/metrics/decorator.py", line 18, in wrapper
  File "compose/cli/main.py", line 1186, in up
  File "compose/cli/main.py", line 1182, in up
  File "compose/project.py", line 702, in up
  File "compose/parallel.py", line 108, in parallel_execute
  File "compose/parallel.py", line 206, in producer
  File "compose/project.py", line 688, in do
  File "compose/service.py", line 581, in execute_convergence_plan
  File "compose/service.py", line 503, in _execute_convergence_recreate
  File "compose/parallel.py", line 108, in parallel_execute
  File "compose/parallel.py", line 206, in producer
  File "compose/service.py", line 496, in recreate
  File "compose/service.py", line 615, in recreate_container
  File "compose/service.py", line 334, in create_container
  File "compose/service.py", line 922, in _get_container_create_options
  File "compose/service.py", line 962, in _build_container_volume_options
  File "compose/service.py", line 1549, in merge_volume_bindings
  File "compose/service.py", line 1579, in get_container_data_volumes
KeyError: 'ContainerConfig'
[4342] Failed to execute script docker-compose

Things I’ve tried:

  • Used an old laptop to start with and got the same results
  • Used my NUC with a fresh install of latest Ubuntu same results
  • Different USB ports makes no difference
  • I used a clean install of HA then edited the docker-compose in case my backup was causing errors but get the same error message

Here’s my docker-compose:

version: '3.0'

services:
  portainer:
    container_name: portainer
    image: portainer/portainer-ce
    restart: always
    ports:
      - "9000:9000/tcp"
    environment:
      - TZ=Europe/London
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /opt/portainer:/data

  homeassistant:
    container_name: homeassistant
    image: "ghcr.io/home-assistant/home-assistant:stable"
    volumes:
      - /opt/homeassistant/config:/config
      - /etc/localtime:/etc/localtime:ro
    devices:
      - /dev/ttyUSB0:/dev/ttyUSB0
    restart: unless-stopped
    network_mode: host

This amounts to my entire knowledge of docker. I use GPT to help me through but seem to have exhausted every troubleshooting step and ended up going down a very big rabbit hole before I gave up.

I followed this guide for install and this for USB mapping.

I noticed you fo not have…
privileged: true
I know many say not to do this but the link you provided shows this, I am using this, and I think it is required to access USB devices.
Worth a try, if you find another way, pass it along.

I generally create a user and group for each container on the host system then provide that user permission for the devices and folders it requires. Running “privileged” and root is the fastest way to verify if permissions are an issue. Also, ask others and running root,privileged and host mode is the norm.

What device is it?
Zwave runs not root/priviledged
Corals are difficult. I don’t remember what I do here since I’ve used pcie and usb versions

So if you ssh into host should be able to

cd /dev
ls

and see “ttyUSB0” listed

The docs say docker compose should use this config

version: '3'
services:
  homeassistant:
    container_name: homeassistant
    image: "ghcr.io/home-assistant/home-assistant:stable"
    volumes:
      - /PATH_TO_YOUR_CONFIG:/config
      - /etc/localtime:/etc/localtime:ro
      - /run/dbus:/run/dbus:ro
    restart: unless-stopped
    privileged: true
    network_mode: host

Start there and see if it works.

By the way, the command is not docker-compose, it is docker compose. No hyphen.

I forgot to mention I did have the privileged: true line in my code originally but I read a few things saying not to use it, but when I took it out I still got hit with the same error. So on this clean install I left it out, but again tried editing it back in and error, so it seemed no matter what I edited I’d always get that same error.

Anyway, I think I might have stumbled on the solution. I edited the docker-compose.yaml, ran docker-compose up -d, got error, then I just deleted the homeassistant container in the portainer interface, then ran docker-compose up -d again and it started with no error and I’ve since managed to get ZHA working! Seems like it was just an oversight on my part, not really knowing how docker works. Could this be the case?

I did end up updating my docker-compose to:

version: '3.0'

services:
  portainer:
    container_name: portainer
    image: portainer/portainer-ce
    restart: always
    ports:
      - "9000:9000/tcp"
    environment:
      - TZ=Europe/London
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /opt/portainer:/data

  homeassistant:
    container_name: homeassistant
    image: "ghcr.io/home-assistant/home-assistant:stable"
    volumes:
      - /opt/homeassistant/config:/config
      - /etc/localtime:/etc/localtime:ro
      - /run/dbus:/run/dbus:ro
    devices:
      - /dev/ttyUSB0:/dev/ttyUSB0
    restart: unless-stopped
    privileged: true
    network_mode: host

@nickrout where did you find the - /run/dbus:/run/dbus:ro line? I couldn’t find it in any documentation. Also tried your no hyphen command but had no success, but potentially that was because I didn’t remove the container like I mentioned earlier. I am on v2.25.0. Maybe I’ll try it again when I add more containers, I see in the link you shared it does indeed say to use a space instead of hyphen for v2.

I don’t know which docs you are looking at, but these are the docs you should be looking at https://www.home-assistant.io/installation/generic-x86-64#docker-compose

1 Like