How to Pass Through a USB to a Docker Container Using Docker Compose

Introduction

In this post, we’ll explore a practical guide on how to give a Docker container access to a USB device using Docker Compose. This can be particularly useful for applications that need to interact directly with hardware connected to the host machine.

Prerequisites

Before proceeding, ensure that you have Docker and Docker Compose installed on your system. Familiarity with basic Docker concepts and command line operations is also recommended.

Step-by-Step Guide

Step 1: Identify the USB Device

First, identify the USB device you wish to pass through. This can be done using commands like lsusb or dmesg in Linux. These commands help you find the device’s bus and device numbers.

Step 2: Modify the docker-compose.yml File

Next, modify your docker-compose.yml file to include the USB device. Here’s a sample configuration:

version: '3'
services:
  myservice:
    image: myimage
    devices:
      - "/dev/bus/usb/001/002:/dev/bus/usb/001/002"

Replace myservice with your service name, myimage with your Docker image, and /dev/bus/usb/001/002 with the path to your USB device.

If you’re running CasaOS you can use my script to update the docker compose or you can use the UI.

bash -c "$(wget -qLO - https://raw.githubusercontent.com/bigbeartechworld/big-bear-scripts/master/casaos-docker-compose-update/run.sh)"

Step 3: Running Docker Compose

With the docker-compose.yml file set up, start your Docker Compose:

docker-compose up

This command will launch the container with access to the specified USB device.

Step 4: Ensure Proper Permissions

Make sure the container has the necessary permissions to interact with the USB device. This might involve adjusting permissions on the host for the USB device file.

Conclusion

Passing through a USB device to a Docker container can unlock new capabilities for your containerized applications. By following these steps, you can seamlessly integrate hardware devices with your Docker containers.