modprobe: FATAL: Module i2c-dev not found in balena OS

Hello,
I want to use the I2C device in Docker Container. (My Device is RaspberryPi3)
When I using RUN modprobe i2c-dev, is failing to start with the following error message:
modprobe: FATAL: Module i2c-dev not found in directory /lib/modules/5.15.32-v7l+

My full Dockerfile:
FROM balenalib/raspberrypi3-python:3.7-latest
ENV PYTHONUNBUFFERED
RUN pip install -r ./requirements.txt
CMD modprobe i2c-dev && python /app/app.py
All input much appreciated, thanks!

Hi, as per the docs, after your first push, the code will most likely throw an error caused by the modules not being loaded. If this is the case, simply reboot the pi and the modules should be loaded. Please refer this. Here’s a neat example for your reference : https://github.com/balena-io-playground/balena-rpi-py-AD , I hope that helps.

Hi @iayanpahwa ,
Thanks for checking in.
I try to reboot my raspberrypi3 and run again the image, but i got the same error.
I try the two thing forward:
1. Run modprobe i2c-dev in docker Container (Run exec)
modprobe: FATAL: Module i2c-dev not found in directory /lib/modules/5.15.32-v7l+
2. Run modprobe i2c-dev in Raspberry PI 3 ← is work
I read the /etc/modules, as shown below:
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with “#” are ignored.
i2c-dev

But It's not work in docker container.

Hi,

I just tried this on my own device.
Assuming: freshly flashed pi3, balenaOS 2.98.33, local development mode.

First I got an error about the copied Dockerfile, saying that ENV PYTHONUNBUFFERED requires a value.
To prevent this, and any pip issues, I changed the Dockerfile to its minimal form:

FROM balenalib/raspberrypi3-python:3.7-latest
CMD balena-idle

Logging in through ssh and running lsmod | grep i2 shows the driver is already loaded.
Disabling local mode and switching the device to production mode didn’t change this.

Changing the Dockerfile to CMD modprobe i2c-dev && balena-idle likewise didn’t cause any issues.

Can you share which OS version you’re running and what your device configuration looks like?

Hi @TJvV ,
Thanks for checking in.
Sorry I loss the value in dockerfile. (ENV PYTHONUNBUFFERED 1)

I install the docker in raspberrypi, and run docker container using belena os.
When I run the belena container, I got the error message.
(modprobe: FATAL: Module i2c-dev not found in directory /lib/modules/5.15.32-v7l+)

My full Dockerfile:

FROM balenalib/raspberrypi3-python:3.7-latest
ENV PYTHONUNBUFFERED 1
RUN pip install -r ./requirements.txt
CMD modprobe i2c-dev && python /app/app.py

My device config in raspberrypi, as shown below:

 lsmod | grep i2

i2c_brcmstb            16384  0
i2c_bcm2835            16384  0
i2c_dev                20480  0
# For more options and information see
# http://rpf.io/configtxt
# Some settings may impact device functionality. See link above for details

# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16

# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720

# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1

# uncomment to force a specific HDMI mode (this will force VGA)
#hdmi_group=1
#hdmi_mode=1

# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
#hdmi_drive=2

# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4

# uncomment for composite PAL
#sdtv_mode=2

#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800

# Uncomment some or all of these to enable the optional hardware interfaces
dtparam=i2c_arm=on
#dtparam=i2s=on
dtparam=spi=on

# Uncomment this to enable infrared communication.
#dtoverlay=gpio-ir,gpio_pin=17
#dtoverlay=gpio-ir-tx,gpio_pin=18

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

# Automatically load overlays for detected cameras
camera_auto_detect=1

# Automatically load overlays for detected DSI displays
display_auto_detect=1

# Enable DRM VC4 V3D driver
dtoverlay=vc4-kms-v3d
max_framebuffers=2

# Disable compensation for displays with overscan
disable_overscan=1

[cm4]
# Enable host mode on the 2711 built-in XHCI USB controller.
# This line should be removed if the legacy DWC2 controller is required
# (e.g. for USB device mode) or if USB support is not required.
otg_mode=1

Hi,

I’ve managed to reproduce your issue.

It seems this happens when you run the balena image on a Raspbian OS rather than on Balena OS.

thijs@tjvv-pi3:~/balena/i2c-dev $ docker build -t balena_i2c .
[...]
thijs@tjvv-pi3:~/balena/i2c-dev $ docker run -it --rm balena_i2c /bin/bash
root@7e86e7842b42:/# modprobe i2c-dev
modprobe: FATAL: Module i2c-dev not found in directory /lib/modules/5.15.32-v7+
root@7e86e7842b42:/#

The problem has to do with some things the balena engine does, that docker doesn’t.
Specifically, balena engine will treat a single service deployment as privileged, and will use certain tags to configure extra things for your container, like mounting devices and modules.

In this specific case, I made it work by using the following command to run the image:

docker run -it --rm -v /lib/modules:/lib/modules balena_i2c /bin/bash 
thijs@tjvv-pi3:~/balena/i2c-dev $ docker run -it --rm -v /lib/modules:/lib/modules balena_i2c /bin/bash
root@a29de6881109:/# modprobe i2c-dev
root@a29de6881109:/#

Do keep in mind that these images were made with the Balena OS and engine in mind, not Raspbian and docker.

Some extra info on the images, Dockerfiles in Balena and multiple services.

1 Like

Hi @ianw, if your application is using a docker compose file you will need to include the io.balena.features.kernel-modules label so the supervisor mounts the /lib/modules directory in the container so that it is accessible to modprobe.