2

My use case:

I use a containerized octoprint instance to drive a 3dprinter. The problem I'm running into is that if the printer(usb) isn't connected during the boot process of the container, then it doesn't boot at all. Docker compose can't find the usb device when it's not connected which causes the container to not boot.

The problem is that the printer is physically turned off when it's not actively printing and turning the printer on every time when the container needs a reboot is not a feasible solution. Is there a way (within docker compose land) to boot the container when a device is not available and then link it once it comes available? OR check if the device is present and if it's not then just boot without it. The host OS I'm using is raspberry pi OS.

The way I have it configured right now is like this:

version: '3'
services:
  octoprint:
    container_name:octoprint
    image: "octoprint/octoprint"
    devices:
      - "/dev/ttyUSB0:/dev/ttyUSB0"
    network_mode:host
    restart: unless-stopped

I can't figure out a way to do this.

The only thing I can think of is to create a script that fires when /dev/ttyUSB0 is created and then add it to the already running container. But this seems like a worst case solution. I would like to solve this within the docker/docker compose framework.

One other "solution" is to automatically reboot the container, every time the printer gets turned on through a homeassistant integration. This is also a worst case scenario solution which I would like to avoid.

Also I'm not quite sure If I should post this on the "superuser" branch of stackexchange or the unix/devops branch. Please correct me if I should post this elsewhere.

DevShot
  • 783
  • 1
  • 8
  • 13
  • What have you tried and what research have you done? – mashuptwice Dec 06 '22 at 19:21
  • 1
    [privileged mode and mounting the whole /dev directory could work](https://forums.balena.io/t/docker-container-cannot-access-dynamically-plugged-usb-devices/4277). Note that privileged mode has security implications. – mashuptwice Dec 06 '22 at 19:23
  • @mashuptwice That's the thing I'm clueless and can't find anything in the documentation or online. I tried to create a symlink to the device and linked that in "devices" in my compose.yml. In the hopes that docker would only check if the file exists and moves on. This is (like is suspected) not the case as docker immediately tries to link the actual device to the container. Regarding your second comment, I'll give it a try. – DevShot Dec 06 '22 at 20:11
  • @mashuptwice It works, but it'll have to be a temporary fix as I don't like the privileged mode being enabled on my docker container. Thanks for the suggestion. – DevShot Dec 06 '22 at 20:22
  • Would it be an option to watch for the USB device, then restart the docker container? – mashuptwice Dec 06 '22 at 20:27
  • Yes, this is probably what I'll end up doing if there's no way to handle this with docker itself. – DevShot Dec 08 '22 at 06:24

1 Answers1

2

As a workaround you can use privileged mode and mount the whole /dev directory to your container:

privileged: true
    devices:
      - '/dev:/dev'

source

mashuptwice
  • 2,929
  • 2
  • 12
  • 25