brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker run --device hands one host device to a container

Docker
~/tips/tip-docker-run-device

docker run -d --name zigbee --device=/dev/ttyUSB0:/dev/ttyUSB0 example/zigbee:1.0

Gives a container access to one host device, like a USB stick, without using privileged mode.

A Zigbee stick or a GPU does not belong in a container that runs as --privileged. Passing just that one device is the tidier route, and it is a single flag.

The syntax in the reference is --device=<host-path>:<container-path>, with an optional third field for permissions. A bare path such as --device=/dev/sdd is valid too.

docker run -d --name zigbee --device=/dev/ttyUSB0:/dev/ttyUSB0 example/zigbee:1.0

The default permissions are read, write and mknod. To restrict them, add the third field with r, rw, m or rwm, for example --device=/dev/ttyUSB0:/dev/ttyUSB0:rw.

The docs also say that if a container needs something special, it is better to grant individual kernel capabilities with --cap-add than to use --privileged. I agree, because privileged gives away far more than most apps need.

The device path has to exist when the container starts. USB devices can get a different number after you unplug them or reboot, so on a real machine I would use the stable name from /dev/serial/by-id/ as the host path instead of ttyUSB0. And privileged containers ignore the permission field altogether, so do not mix the two. Check that the container’s user is allowed to open the device, which may need a group added with --group-add.

Further reading: docker run reference: device

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link