Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose: Ignore device map if device not found

When defining a device map like this in docker-compose.yml:

version: "3.8"

services:
  app:
    build: .
    devices:
      - /dev/video0:/dev/video0

If you start this container without /dev/video0 on your host system, the container will crash with the following error:

Error response from daemon: error gathering device information while adding custom device "/dev/video0": no such file or directory

  1. Is it possible to start the container, ignoring the error, if the device is not present?
  2. If I would later connect the device while the container is running, is it possible to automatically mount it into the container as well?
like image 329
zjeffer Avatar asked Dec 10 '25 02:12

zjeffer


1 Answers

  1. Is it possible to start the container, ignoring the error, if the device is not present?

No

  1. If I would later connect the device while the container is running, is it possible to automatically mount it into the container as well?

No, these mounts can not be changed after startup.

Possible solution:

I would prefer to create a network outside docker-compose, so that we can control when it's available and when not.

Change the compose file so that all other services also use that network

I would create a script that looks to which devices are available, and then use dynamically use docker run --network YOUR_NETWORK and add --device for each device that you discovered that is available.

If you would like to change devices automatically, you can think of a script that watches if devices change, and then stop the running container and start a new one using the created script. Though this will cause a short downtime.

like image 91
Dennis van de Hoef Avatar answered Dec 16 '25 23:12

Dennis van de Hoef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!