Delay systemd service startup until nfs shares mount

xrapidx

Honorary Master
Joined
Feb 16, 2007
Messages
42,219
Reaction score
4,062
Location
Cape Town
I have two machines, a server, and a NAS.

I'm running Ubuntu 20.04 on my server, with the return of load shedding, I've noticed the server starts up before the NAS - this kicks off the systemd docker service - which loads various containers.

Some of the containers are shutting down (or going into a safe mode) because they expect certain nfs mounts to be available.

I'd like to add something to the docker service so it waits for the nfs mounts - opening snap.docker.dockerd.service - I'm confronted with this line:
[Unit]
# Auto-generated, DO NOT EDIT

So - my question is - how can I delay the service startup until /mnt/media is available?
 
How are the containers started?
Is NFS being handled by systemd?
 
How are the containers started?
Is NFS being handled by systemd?
Looks like they're started by the docker service.

NFS - yes.

I've created an override.conf file - but it seems to be ignored.
 
I assume your override.conf looks something like this?
Code:
UPDATE / SOLUTION FOR ANYONE HAVING THIS PROBLEM
1.) Make sure your NFS mount in /etc/fstab does NOT have the "bg" background field
and
2.) Create: /etc/systemd/system/docker.service.d/override.conf
with:

[Unit]
After=nfs.mount
and then
systemctl daemon-reload

and then reboot.

This is needed until docker adds "After=nfs.mount" to the default docker Unit section.

^^^ UPDATE / SOLUTION FOR ANYONE HAVING THIS PROBLEM ^^^

Found few forums/pages where people discusses the same issue.
 
Yip... although, I'm making some progress

I added the Requires, now it fails to start
Code:
[Unit]
#Requires=
#Requires=snap-docker-471.mount mnt-Media.mount
Requires=mnt-Media.mount
After=mnt-Media.mount
 
Looks like adding the requires solved the issue - it fails initially, but restarts 30 seconds later. (set Restart=on-failure)
 
How are you mounting your NFS shares? Mine's via /etc/fstab so mounted onto the OS, I haven't had to change anything, initial boot will wait for NFS to become available.

Alternatively stagger your machines startups, one easy way to do it would be via Wake on LAN, have your NAS send your server a WOL package once it's started.
 
create your own systemd service file (eg: start-nfs-docker.service) and point to a bash file.
Bash:
[Unit]
Description=Start NFS and Docker
After=network.target

[Service]
ExecStart=/usr/local/bin/start-nfs-docker.sh

[Install]
WantedBy=multi-user.target

In the bash file, mount your nfs share and then start the docker service.

Then enable your service and disable the docker one from startup.
Bash:
systemctl enable start-nfs-docker.service
systemctl disable docker.service

Change your /etc/fstab entry for the nfs share and add "noauto" as an option. (as the bash script it handling the mounting of the nfs share).

You can even get creative and add a check in your bash script to ensure that the nfs share is mounted before starting the docker service.
 
Top
Sign up to the MyBroadband newsletter
X