Docker starting before Virtualbox shares are mounted

My setup: Virtualbox with a Windows Host and Ubuntu Server guest. Ubuntu is now 21.04 but the issue existed before upgrade from 20.04. And on the Ubuntu Server I run Docker.

The issue: The shares from virtualbox, which I think are mounted by the Vbox guest addons are not mounted until after Docker starts. The containers start OK as their config is on /home which is a local drive, however they have data on a share. Because Docker starts before the shares are mounted the containers bind to the mountpoint as a directory rather than a mount and they can write to the disk there filling up the root filesystem with invisible files.

I hadn't made any changes recently other than keeping up with Ubuntu updates but I feel like I might have put a sleep somewhere in the past to get around this. I could look to do this again but it seems rather inelegant and I wondered if there might be a better way through systemd or otherwise.

I've tried a number or systemd directives without any luck. RequiresMountsFor and ConditionPathIsMountPoint were the two that I thought might work. I also tried to tell the docker service to start after the vbox guest service but this didn't work either.

Any suggestions?


The solution was to override the systemd service file for containerd.

run

    systemctl edit containerd

add the following in the place specified in the comments.

    [Unit]
    After=
    After=network.target local-fs.target vboxadd-service.service
    Requires=vboxadd-service.service

The first After= clears that directive ready for the new one. Both After and Requires are needed. Save this file and then restart host.

Works without issue and confirmed by 'systemd-analyse plot', which is a very useful command for interrogating your systemd run order. This method will survive software updates