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

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
RUN pip install -r ./requirements.txt
CMD modprobe i2c-dev && python /app/
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 : , 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.

But It's not work in docker container.


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
RUN pip install -r ./requirements.txt
CMD modprobe i2c-dev && python /app/

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
# Some settings may impact device functionality. See link above for details

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

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

# uncomment to force a console size. By default it will be display's size minus
# overscan.

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

# uncomment to force a specific HDMI mode (this will force VGA)

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

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

# uncomment for composite PAL

#uncomment to overclock the arm. 700 MHz is the default.

# Uncomment some or all of these to enable the optional hardware interfaces

# Uncomment this to enable infrared communication.

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

# Enable audio (loads snd_bcm2835)

# Automatically load overlays for detected cameras

# Automatically load overlays for detected DSI displays

# Enable DRM VC4 V3D driver

# Disable compensation for displays with overscan

# 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.


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+

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

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”

- io.balena.features.kernel-modules
build: .
- 3000:3000
restart: always
- /lib/modules:/lib/modules
privileged: true
- “ALL”
- /dev/mem:/dev/mem
- /dev/gpiomem:/dev/gpiomem
- /dev/i2c-1:/dev/i2c-1


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)


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