Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making/Building a kernel module inside a docker container

I currently have my host system running Mac OS with docker. I have my Mac OS host system spawning a docker container.

The spawned docker container is currently running ubuntu:19.10

I am trying to build a kernel module inside the docker container

When I run

$> make

I get...

Building coolMod driver...
make -C /lib/modules/`uname -r`/build M=/home/foo/coolMod modules
make[1]: *** /lib/modules/4.19.76-linuxkit/build: No such file or directory.  Stop.
make: *** [Makefile:43: coolMod.ko] Error 2

The docker container does not have the kernel headers.

When I try to run:

$> apt install linux-headers-$(uname -r)
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package linux-headers-4.19.76-linuxkit
E: Couldn't find any package by glob 'linux-headers-4.19.76-linuxkit'
E: Couldn't find any package by regex 'linux-headers-4.19.76-linuxkit'

How can I install kernel headers so I can build a kernel module from within my Docker container?

Thank you!

like image 896
jremi Avatar asked May 31 '26 05:05

jremi


1 Answers

Docker containers don't have a kernel of their own; they use the kernel of the Docker host. So when you run uname command, the result you are seeing is that of the underlying Docker host. You can verify this by comparing the result of uname -a on the Docker host to the result in the Docker container.

This most probably the reason why your make as well as the apt install both are failing.

I'm not sure about the specific module you are trying to build and if it has any other dependencies. In general, you can build a kernel module in the Docker container if you have the kernel source in your Docker container. Try using a specific version of the kernel in your apt install command instead of using/relying on uname command. Then $ make -C <path_to_kernel_src> M=$PWD

Refer to this answer I just posted on another question.

like image 62
Praveen Lobo Avatar answered Jun 02 '26 19:06

Praveen Lobo