Search code examples
dockerfreebsd

Can FreeBSD be run inside Docker?


I have been looking for a Docker image of FreeBSD but cannot find, can FreeBSD be run inside docker? If not, why not?


Solution

  • There's a lot of caveats here but yes, you can and no its not depended on FreeBSD (but openbsd may not work).

    #!/bin/sh
    
    wget https://download.freebsd.org/ftp/releases/ISO-IMAGES/13.2/FreeBSD-13.2-RELEASE-amd64-bootonly.iso
    
    cat -> docker-compose.yml <<< EOL
    ---
    version: "3"
    services:
        freebsd-via-qemu:
            image: jkz0/qemu:latest
            cap_add:
                - NET_ADMIN
            devices:
                - /dev/net/tun
                - /dev/kvm
            volumes:
                - ./FreeBSD-13.2-RELEASE-amd64-bootonly.iso:/image
            restart: always
    ...
    EOL
    docker-compose up -d
    

    The above shell script would download the boot only iso for FreeBSD then spin up a QEMU container which in turn boots the os. I do this with other systems like Plan9, SimH, MVS, Freenas, AROS, and VyOS. And yes this does work with Podman, WSL, or K8S too.

    Now for the caveat here, this is clearly not a hypervisor running in Ring 0, but a x86 emulator running in user space and jailed off at that. Your not going to get any performance out of this setup and may see some issues with device drivers or worse just outright be unusable for anything other than as a toy/research.

    Update:

    Reliased I didn't answer OP's second question; the Why not. Docker at its fundamental is just a wrapper with a RestAPI daemon for core Linux kernel tech such as cgroup, NetNS, vxlans, proc namespaces, user namespaces, and unionfs. e.g. the bulk of what Plan9 has had since 1991 just redone by crunchy Linux Kernel devs.

    Yes BSD has jails but that's userspace as a chroot not kernel space such as Linux or proc/stack space as Plan9. [This may have changed in Open or FreeBSD in the last 15 or so years]

    Basically, a native bsd kernel or userspace inside a namespace stack of the Linux kernel space is not able to execute without an emulator or hypervisor. It's the same issue on WSL (aka hyper-v) and xhyve (OSx's hypervisor). This is why QEMU works in the example above. Its that hypervisor that loads a BSD kernel and operates within the container's namespace.