Running pulseaudio inside docker container to record system audio

I’m trying to set up a Docker container with Selenium that takes a recording of the browser with system audio using ffmpeg. I’ve got video working using Xvfb. Unfortunately, on the audio side, it seems to be more tricky.

I thought I would set up a virtual pulseaudio sink inside the container, which would allow me to record its monitor:

pacmd load-module module-null-sink sink_name=loopback
pacmd set-default-sink loopback
ffmpeg -f pulse -i loopback.monitor test.wav

This works on my host operating system, but when trying to start the pulseaudio daemon in a container, it fails with the following message:

E: [pulseaudio] module-console-kit.c: Unable to contact D-Bus system bus: org.freedesktop.DBus.Error.FileNotFound: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory

This would seem to be related to a freedesktop service called dbus. I’ve tried installing it and starting its daemon, but I couldn’t seem to get it to work properly.
I couldn’t find much information on how to proceed from here. What am I missing for pulseaudio? Perhaps there’s an easier way to record the system audio inside a container?

My goal is not to record it from the host operating system, but to play the audio inside the browser and record it all inside the same container.

Source: Docker Questions