24

Is it possible to use a bluetooth (BLE in my case) dongle inside of a docker container?

On my host machine:

$ hcitool dev
    Devices:
       hci0   5C:F3:70:64:F0:11

Inside of Docker it doesn't find anything. I'm running Docker as:

sudo docker run --privileged -i -t ubuntu /bin/bash

I don't know enough about the bluetooth subsystem in Linux to understand what is different between the host and docker.

The hci0 device does show up in both systems:

$ ls -l /sys/class/bluetooth
lrwxrwxrwx  1 root root 0 Mar  5 01:23 hci0 -> ../../devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-2/2-2.3/2-2.3:1.0/bluetooth/hci0

Anyone try to use bluetooth inside of Docker?

OlivierM
  • 2,820
  • 24
  • 41
Brad
  • 5,845
  • 7
  • 30
  • 29
  • You can mount the USB device in your container. See [this answer](http://stackoverflow.com/a/24231872/2430241). – Ben Whaley Mar 05 '15 at 04:21
  • 4
    There's an open issue on Github here: https://github.com/docker/docker/issues/16208 – vielmetti Sep 11 '15 at 14:43
  • You may want to check [this answer](http://stackoverflow.com/a/24231872/275195) for a question about how to give access to USB devices. – Pascal May 17 '16 at 11:06

5 Answers5

12

If you want to run bluez from a docker (and not only expose hci adapter) you need:

  • To start your docker with sudo docker run --privileged -i -t your_image_name /bin/bash
  • Make sure bluez is not running on your host. In my case I add to kill bluez (killall -9 bluetoothd) (and not stopped it properly as it will power down my bluetooth adapter and will not exposed it into the docker)
  • In your docker entrypoint, you will need to start dbus (/etc/init.d/dbus start) and bluez (/usr/libexec/bluetooth/bluetoothd --debug &)
OlivierM
  • 2,820
  • 24
  • 41
10

I can confirm that what OlivierM wrote is working on me. Spent some time on Raspberry Pi 3B+ and its built-in bluetooth.

Dockerfile:

FROM python:3.7

RUN apt-get update

RUN apt-get install -y bluez bluetooth

ENTRYPOINT sh docker_entrypoint.sh

and entrypoint:

#!/bin/bash

service dbus start
bluetoothd &

/bin/bash

sudo killall -9 bluetoothd is needed on host machine before before starting the container:

docker run --rm --net=host --privileged -it myimage:mytag
jukkei
  • 111
  • 1
  • 4
9

Try this:

sudo docker run --net=host --privileged -i -t ubuntu /bin/bash
ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103
daparic
  • 3,794
  • 2
  • 36
  • 38
7

I found that there are two ways to do this:

  1. Shared access to bluetooth between host and container. This mode is done by sharing /var/run/dbus as a volume mount with the container. This means that the container will use the dbus from the host as well. The container should not start its own dbus or bluetooth service - this should be done in the host. It makes the docker_entrypoint.sh script redundant. A tutorial showing this approach is available at https://www.raspberrypi-bluetooth.com/bluetooth-application-in-python.html

    Approach:

    • Install bluetooth packages you need in container
    • Make sure bluetooth service is running on your host OS
    • run your container with the volume mount and maybe with --privileged:
      docker run -v /var/run/dbus/:/var/run/dbus/:z --privileged {containerImage}
      
  2. Exclusive access to bluetooth by the container. This requires stopping the bluetooth service in the host (otherwise the container can't "claim" it). This method can be followed by the answers above

hansmbakker
  • 1,108
  • 14
  • 29
0

With help from Docker Community, I have Successfully Started DBus/Bluetooth Services inside docker container and scan bluetooth devices by adding --cap-add=SYS_ADMIN, --cap-add=NET_ADMIN and --net=host flags/permission

docker run --cap-add=SYS_ADMIN --cap-add=NET_ADMIN --net=host -it debian:jessie

Now just looking to start bluetooth service by "Not sharing the Host Networking Namespace" (customising --net=host to private network)

If somebody got any Clue, that would be helpful. Thanks.