5

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!

jremi
  • 2,879
  • 3
  • 26
  • 33
  • A kernel module is pretty specific to the kernel it's built for, and usually a container can't load or unload modules. Where are you hoping to run the module? Do you know the exact kernel version it will have? – David Maze Jun 18 '20 at 17:20
  • Basically i'm trying to use a module called https://github.com/umlaeute/v4l2loopback ... It creates a virtual video device. However, I keep running into the issue of actually being able to load the module inside the container to make the driver available. – jremi Jun 18 '20 at 17:25
  • 2
    Docker containers can't load kernel modules. You need a virtual machine with an isolated kernel for that. – David Maze Jun 18 '20 at 17:28
  • Yep, that is also what i have concluded. I am in the process now of setting up a vm. thx – jremi Jun 18 '20 at 17:29
  • @DavidMaze do you have a source / further reading on that? – haelix May 21 '21 at 08:10

3 Answers3

0

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

0

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.

Praveen Lobo
  • 6,956
  • 2
  • 28
  • 40
0

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.

J. Blackadar
  • 1,821
  • 1
  • 11
  • 18