User Tools

Site Tools


make_docker_or_any_other_service_wait_on_nfs_mounts

Make Docker (or any other service) wait on NFS mounts

I have an Ubuntu 22.04 server that uses NFS to mount a media server share, and it uses docker containers to fetch new media. Prior to systemd, everything just worked, because NFS mounts blocked boot. Because systemd people don't understand Linux that well, they broke this.

To fix it, you can patch in additional dependencies for services without editing the main service definition. To add new rules for a service, make a directory /etc/systemd/system/docker.service.d, and under it, a file like nfs-mounts.conf.

In this file, you can put any new rules/dependencies you want, but the easiest thing is to use the RequiresMountsFor directive, then specify a space-delimited list of mountpoint directories that systemd must wait on before starting docker (e.g. "/x/content /x/apps").

In total, the solution is just:

$ mkdir /etc/systemd/system/docker.service.d
$ nano /etc/systemd/system/docker.service.d/nfs-mounts.conf
[Unit]
RequiresMountsFor=/x/content /x/apps

On next reboot, the system should now block docker until NFS comes up.

Setting boot-time mount timeout

The default mount timeout on boot appears to be 95 seconds in Ubuntu 22.04. If your NFS server is slower than that to reboot, you might fail to mount after an outage.

To set the mount timeout in systemd world, add the option "x-systemd.mount-timeout=500" to the /etc/fstab entry. Example:

192.168.0.11:/volume1/content  /x/content  nfs4 rw,_netdev,x-systemd.mount-timeout=500 0 1

Hey, do you want to know something horrifying and stupid about systemd?

If you use the diagnostic command "systemctl show" followed by literally any service name, it prints out a long list of plausable settings as if that service actually exists, even if it's completely made up!

$ systemctl show POOPSERVICE9000 | head
Restart=no
NotifyAccess=none
RestartUSec=100ms
TimeoutStartUSec=1min 30s
TimeoutStopUSec=1min 30s
TimeoutAbortUSec=1min 30s
...etc...

This makes debugging super cool and fun. Good job, guys.

make_docker_or_any_other_service_wait_on_nfs_mounts.txt · Last modified: 2023/11/19 20:21 by tkbletsc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki