Testing the Coral TPU Accelerator (M.2 or PCIe) in Docker

Google Coral TPU in PCIe carrier

I recently tried setting up an M.2 Coral TPU on a machine running Debian 12 'Bookworm', which ships with Python 3.11, making the installation of the pyCoral library very difficult (maybe impossible for now?).

Some of the devs responded 'just install an older Ubuntu or Debian release' in the GitHub issues, as that would give me a compatible Python version (3.9 or earlier)... but in this case I didn't want to do that.

So the next best option would be to set up the PCIe device following the official guide (so you can see it at /dev/apex_0), then pass it through to a Docker container—which would be easier to set up following Coral's install guide.

Install Docker

I installed Docker using the instructions provided for an apt-based install on Debian:

sudo apt install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Build a Docker image for Coral testing

Create a Dockerfile with the following contents:

FROM debian:10

ENV HOME /home
RUN cd ~
RUN apt-get update
RUN apt-get install -y git nano python3-pip python-dev pkg-config wget usbutils curl

RUN echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" \
| tee /etc/apt/sources.list.d/coral-edgetpu.list
RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
RUN apt-get update
RUN apt-get install -y edgetpu-examples

It's important to use Debian 10, as that version still has a system Python version old enough to work with the Coral Python libraries.

Build the Docker image, and tag it coral:

sudo docker build -t "coral" .

Run the Docker image and test the TPU

Make sure the device /dev/apex_0 is appearing on your system, then use the following docker run command to pass that device into the container:

sudo docker run -it --device /dev/apex_0:/dev/apex_0 coral /bin/bash

(If you're in the docker group, you can omit the sudo).

This should drop you inside the running container, where you can run an Edge TPU example:

container-id# python3 /usr/share/edgetpu/examples/classify_image.py --model /usr/share/edgetpu/examples/models/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite --label /usr/share/edgetpu/examples/models/inat_bird_labels.txt --image /usr/share/edgetpu/examples/images/bird.bmp

This should work... but in my case I was debugging some other flaky bits in the OS, so it didn't work on my machine.

Special thanks to this comment on GitHub for the suggestion for how to run Coral examples inside a Docker container.


I use the USB accelerator for a Frigate (camera) install, it has the same performance and wider compatibility. The only thing that needs to be mapped is `--device /dev/bus/usb:/dev/bus/usb`.

> I recently tried setting up an M.2 Coral TPU on a > machine running Docker 12 'Bookworm'
Did you mean Debian 12 ‘Bookworm’?

The coral tpu in the picture - is that the dual tpu coral? What is the pcie adapter to which it is connected? Can you share the link to the pcie adapter, please?


That's a single TPU; I have a dual, but it requires a rather exotic M.2 slot that most motherboards don't include.

The adapter board I'm using is a PCIe to A+E adapter, similar to this one.

Thank you I bought a tpu months ago but couldn't find the correct adapter that I trusted that was the mini pcie to pcie 1x was way more difficult than I had expected

I recently, similarly, passed through the USB one into an lxd container for the same reason. It looks like the Coral team has abandoned it in the last 6 months.

Dual accelerator requires either full m.2 slot (not CNVe or whatever the name is) or adapter. Amazon will have 50% which won't work at all and 50% which will pass single chip only. There is a hope: search for Coral TPU adapter on Makerfabs. Got one of those. Now 2 chips are visible and fully functional.

Couldn't you just use miniconda to create an environment for the coral and install Python 3.9 and libraries that way? It installs in the home directory and can easily be removed but you get your full native system to use.