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.

I am trying to get this to work (I can’t tell if the OP was able to). I followed everything in this thread. As per your example, I have my Dockerfile at the bare minimum. Same balenalib image. I can also ssh in and run lsmod | grep i2 to find the drivers are loaded. However when I add modprobe i2c-dev to the CMD I still receive

modprobe: FATAL: Module i2c-dev not found in directory /lib/modules/5.15.34-v8

This is on a pi4 with a fresh BalenaOS v2.108.18+rev2 burn.
I’m using a docker-compose file which is below.
config.txt was also modified to enable i2c as per the OP’s comments
If someone can spot what I am missing I would be really grateful!

version: “2”

services:
shank-sensor:
labels:
- io.balena.features.kernel-modules
build: .
ports:
- 3000:3000
restart: always
volumes:
- /lib/modules:/lib/modules
privileged: true
cap_add:
- “ALL”
devices:
- /dev/mem:/dev/mem
- /dev/gpiomem:/dev/gpiomem
- /dev/i2c-1:/dev/i2c-1

Hi,

I’d be happy to look into this tonight or tomorrow.
Can you post your full config.txt, Dockerfile and docker-compose.yml as preformatted text (rather than blockquote)?
That would make it easier for me to reproduce your situation.

Hello TJvV, thanks for the quick response. The files are here.
On a related note, I read that balena by default enables SPI protocol on a pi. Is this correct?
Thank you
config.txt (35.5 KB)
docker-compose.txt (365 Bytes)
Dockerfile.txt (222 Bytes)

Hi,

The docs say SPI is enabled by default, yes.
You can also the line dtparam=spi=on in your config.txt (line 1110).

I’ve had a little poke around with your setup and can confirm I didn’t see /lib/modules getting mounted at first either.

To make it work, I changed the docker-compose.yml label to read
io.balena.features.kernel-modules: '1' instead of - io.balena.features.kernel-modules.

Note that you don’t need your own volume entry for /lib/modules.

Thanks TJvV! Your solution worked, but there was an error in your response:
The docker-compose.yaml file should have the label

io.balena.features.kernel-modules: 1

As opposed to what you entered, `io.balena.features.firmware: 1’.

Once I changed the file to reflect this it worked!

Thanks again for the quick response! Great first impression for me in this community!

My bad in copy pasting; was testing with the firmware label to see if that one did mount when the modules didn’t :stuck_out_tongue:
I’ve edited the post so the correct answer stands

1 Like