jremi
jremi

Reputation: 2969

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!

Upvotes: 8

Views: 16389

Answers (3)

J. Blackadar
J. Blackadar

Reputation: 1961

In your Dockerfile, you'll need to add the repo for your OS which provides the kernel development resources.

For me, (on ubi8) I first needed to add the Oracle Linux 8 repository and its GPG key:

RUN rpm --import "https://yum.oracle.com/RPM-GPG-KEY-oracle-ol8"
RUN dnf config-manager --add-repo http://yum.oracle.com/repo/OracleLinux/OL8/baseos/latest/x86_64

Then, you can install the development tools and modules:

RUN dnf install kernel-devel-$(uname -r) kernel-modules-$(uname -r) elfutils-libelf-devel -y

Note that this assumes the host is running the same kernel as you are developing a kernel module for.

Upvotes: 0

Praveen Lobo
Praveen Lobo

Reputation: 7187

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.

Upvotes: 0

Juanma Gomez
Juanma Gomez

Reputation: 11

Maybe to late, but in order to build a kernel module inside the container you should execute the container in --priviliged mode.

Apart from the dependencies kernel-devel and kernel-headers, the kernel should be built for your specific kernel version, so you should create a volume to link host:/lib/modules to your_container:/lib/modules.

A good starting point should be https://projectatomic.io/blog/2018/06/building-kernel-modules-with-podman/.

Upvotes: 0

Related Questions