Accessing GPIOs in a Docker container created from a ROS2 image

Hey,

I just started using Docker a week ago to work on my project :baby:. I need to use the MCC128 board in a ROS2 node. However, the board is detected on Raspberry Pi OS and ROS2 on Ubuntu. So, I want to use Docker to have a ROS2 environment on my Raspberry Pi OS.

I’ve looked through a lot of forums, but I still haven’t found a solution to my problem, so I’m hoping to find my savior here…

I’m not sure what information is needed to understand my issue, so I’ll try to be as precise as possible:

My Raspberry Pi → Raspberry Pi 3 B
OS → Raspberry Pi OS Lite 64Bits

delfinRos@raspberrypi:~  $ cat /etc/os-release
   PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
   NAME="Debian GNU/Linux"
   VERSION_ID="12"
   VERSION="12 (bookworm)"
   VERSION_CODENAME=bookworm
   ID=debian
   HOME_URL="https://www.debian.org/"
   SUPPORT_URL="https://www.debian.org/support"
   BUG_REPORT_URL="https://bugs.debian.org/"

delfinRos@raspberrypi:~  $ arch 
   aarch64

delfinRos@raspberrypi:~ $ uname –a 
   Linux raspberrypi 6.12.20+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.12.20-1+rpt1~bpo12+1 (2025-03-19) aarch64 GNU/Linux

To install Docker on my Raspberry Pi, I followed this guide: https://docs.docker.com/desktop/setup/install/linux/debian/ using the apt repository.

I pulled this image from Docker Hub: docker pull arm64v8/ros:humble-ros-base.

Then, I wrote this Dockerfile:

FROM arm64v8/ros:humble-ros-base 
WORKDIR /ros_data 

# Install sudo and useful tools 
RUN apt update && apt install -y \ 
    sudo \ 
    python3-pip \ 
    python3-dev \ 
    python3-rosdep \ 
    python3-colcon-common-extensions \ 
    i2c-tools \ 
    kmod \ 
    libcap-dev \ 
    && rm -rf /var/lib/apt/lists/* 

# Add user delfinRos and permissions as root 
RUN useradd -ms /bin/bash delfinRos \ 
    && echo 'delfinRos:delfinRos' | chpasswd \ 
    && adduser delfinRos sudo \ 
    && echo '%delfinRos ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers 

# Add user to groups required for accessing I2C, SPI, and GPIO 
RUN groupadd -f spi \ 
    && groupadd -f i2c \ 
    && groupadd -f gpio \ 
    && usermod -a -G spi delfinRos \ 
    && usermod -a -G i2c delfinRos \ 
    && usermod -a -G gpio delfinRos 

# Initialize rosdep (run as root to avoid permission issues) 
USER root 
RUN rosdep update 

# Switch back to 'delfinRos' user 
USER delfinRos 

# Copy project files into /ros_data 
COPY . . 

# Set up a volume for persistent storage 
VOLUME ["/ros_data"]

# Ensure ROS 2 environment is sourced on shell startup 
RUN echo 'source /opt/ros/humble/setup.bash' >> /home/delfinRos/.bashrc 

# Default command to start an interactive shell 
CMD ["/bin/bash"]

Then I built the image with:

docker build -t my_ros2_image .

And ran the container:

docker run --privileged --device=/dev/mem --device=/dev/i2c-1 --device=/dev/i2c-2 --device=/dev/spidev0.0 --device=/dev/spidev0.1 --device=/dev/gpiomem -v /home/delfinRos:/ros_data -it my_ros2_image

I’m trying to give Docker as much access as possible to read my MCC128 board, which uses I2C and SPI communication.

Inside my container, I run sudo apt-get update and sudo apt-get upgrade.

Once that’s done, I go to my daqhat folder, which I have access to thanks to the volume in the Dockerfile. It’s a folder I downloaded from this GitHub: https://github.com/mccdaq/daqhats/blob/master/README.md
Following the README, I try to install the board by running sudo ./install.sh.

Everything goes well until the end, where I get the following error:

Reading DAQ HAT EEPROMs

/usr/local/bin/daqhats_read_eeproms: 32: dtoverlay: not found
Loading of i2c-gpio dtoverlay failed. Do an rpi-update (and maybe apt update; apt upgrade).

./install.sh: line 71: raspi-config: command not found
./install.sh: line 71: [: -eq: unary operator expected
Shared library install complete.

So I can’t use the example code from the GitHub folder, and therefore I can’t build a ROS node using the daqhat library. :smiling_face_with_tear:

I’m not sure if what I want to do is possible, but I’m staying positive about finding a genius here!

If there’s any missing information to debug this, I’ll gladly update my post.

So you installed Docker Desktop that runs containers in a virtual machine. That won’t work if you want to mount devices from the host. Install Docker CE