Docker on FreeBSD

I love FreeBSD; I use it on my servers and on my desktop. However, it can be painful to run FreeBSD when you work for companies that use Docker everywhere. Docker is a Linux tool: unlike macOS or Windows, FreeBSD gets no official Docker support. So in this post, I will show you how to build a complete Docker and Docker Compose environment on FreeBSD.

FreeBSD comes with everything needed to run Docker: bhyve, a hypervisor able to run a Linux guest, and virtual networking. The idea is simple: the Docker daemon runs in a small Linux virtual machine, and the Docker client runs on FreeBSD and talks to it.

Prerequisites

A computer running FreeBSD with hardware virtualization support, and these packages installed:

The VM

Let’s begin by creating the Linux virtual machine. I chose Ubuntu because it is a mainstream Linux distribution, but you can use a more exotic one. It does not matter.

vm create -t ubuntu -s 250G -m 8G -c 8 docker

The important thing is to have enough disk space to store Docker image layers. A large disk should not be an issue: on ZFS, the volume is sparse and only takes the space it actually uses.

Next, configure the virtual machine to boot from the right GRUB partition, and to share a filesystem with the FreeBSD host. The shared filesystem is what makes it possible to mount a local directory in a container.

vm configure docker

The diff shows my changes to the virtual machine configuration:

 loader="grub"
+# Ubuntu installs GRUB on the second partition.
+grub_run_partition="2"
 network0_type="virtio-net"
 network0_switch="public"
 disk0_type="virtio-blk"
 disk0_name="disk0.img"
+
+# The Docker daemon needs access to the host filesystem to share volumes.
+disk1_type="virtio-9p"
+disk1_name="sharename=/home/gearnode/Workspace"
+disk1_dev="custom"

Then, download the Ubuntu ISO. I chose Ubuntu Server because it is light, and 22.04 is an LTS release, so it does not need a major upgrade for years.

vm iso https://releases.ubuntu.com/22.04/ubuntu-22.04-live-server-amd64.iso

I will not explain how to install Ubuntu, but in the installer, do not use LVM (Logical Volume Management); otherwise, GRUB does not end up on the second partition and the VM does not boot.

vm install -f docker ubuntu-22.04-live-server-amd64.iso

After the installation, I do some personal configuration on the VM (sshd, zshrc, etc.).

Docker daemon

Through the serial console or SSH, I install Docker on the Ubuntu VM. I will not copy the official instructions here.

By default, the Docker daemon only listens on a Unix socket. This is fine when the daemon and the client run on the same host, which is not the case here: the FreeBSD host must be able to reach the daemon in the VM. So I make the daemon listen on TCP as well, in /etc/docker/daemon.json:

{
  "hosts": [
    "tcp://0.0.0.0:2375",
    "unix:///var/run/docker.sock"
  ]
}

I keep the original Unix socket, so the docker CLI keeps working inside the VM without any configuration, and add a TCP socket on port 2375.

NOTE: The TCP socket has no authentication and no encryption, and access to the Docker daemon is root access to the VM. Only expose it on a network you trust, or use SSH instead (see below).

On Ubuntu, the systemd unit of Docker already passes -H fd:// to the daemon, which conflicts with the hosts key. Remove it with an override:

systemctl edit docker
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --containerd=/run/containerd/containerd.sock

Last, I configure the Ubuntu fstab to mount the shared directory at the same path as on the host. This way, a path given to docker run -v on FreeBSD exists in the VM too.

 # /etc/fstab: static file system information.
 #
 # Use 'blkid' to print the universally unique identifier for a
 # device; this may be used with UUID= as a more robust way to name devices
 # that works even if disks are added and removed. See fstab(5).
 #
 # <file system> <mount point>   <type>  <options>       <dump>  <pass>
 # / was on /dev/vda2 during curtin installation
 /dev/disk/by-uuid/20d4ba4f-e6e6-453b-9153-8c06f4a1924f / ext4 defaults 0 1
 /swap.img       none    swap    sw      0       0
+sharename       /home/gearnode/Workspace 9p trans=virtio,rw,_netdev 0 0

Then I restart the VM to make sure everything comes back up.

Docker client

On the FreeBSD side, I only need the client and Compose:

pkg install docker docker-compose

Despite its name, the docker package only contains the client.

The client reads the address of the daemon from DOCKER_HOST. I set it in my shell profile, with the address of the VM:

export DOCKER_HOST=tcp://172.16.2.2:2375

If you prefer not to expose the TCP socket, the client can also go through SSH. The daemon then needs no configuration change at all, only an account in the docker group:

export DOCKER_HOST=ssh://gearnode@172.16.2.2

And that is it:

docker run --rm hello-world
docker-compose up -d

I have used this setup daily on my workstation and laptop to build, run and develop with Docker, without any issue. I hope this post helps people looking to run Docker on FreeBSD.