Start docker service after NFS shares have been mounted

a lot of my containers depend on data that exist on NFS shares. So when restarting the machine, if the docker service starts before all the NFS shares have been mounted, the containers cannot start.

so I need a way to mount all the NFS shares before starting the docker service. So I modified this file

/etc/systemd/system/multi-user.target.wants/docker.service

which contains this

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

to this

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service var-lib-docker-volumes.mount mnt-media.mount mnt-monitor.mount
Wants=network-online.target
Requires=docker.socket

I restarted the machine, but again, the docker service started before the shares were mounted. so I tried this

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket var-lib-docker-volumes.mount mnt-media.mount mnt-monitor.mount

But again after the restart, the docker service started before the NFS shares were mounted.

What is the best way to start the docker service, only after the NFS shares have been mounted?

Thanks

Why are you even trying to mount your nfs shares uing systemd, rather the /etc/fstab? I never had a timing problem with mounts in /etc/fstab - except stale connections from time to time.

Because of the stale connections, I moved over to use a nfs volume, declared in my compose file:

volumes:
  plex-data:
    driver_opts:
      type: nfs 
      o: addr=192.168.1.1,nfsvers=4
      device: :/exported/path

But I am using /etc/fstab to mount the NFS share. Still, the docker service starts before /etc/fstab manages to mount the NFS share. So I just login on the machine run systemctl restart docker and everything is fine.

I also tried your suggestion to mount the share as an NFS volume in docker, like this

docker volume create --driver local --opt type=nfs --opt o=addr=192.168.1.29,rw --opt device=:/media media

and the volume is created under /var/lib/docker/volumes but it’s empty. Shouldn’t it contain the files I have in my NFS share? (\192.168.1.29\media)

Odd that you are having problems with mount in /etc/fstab. Never had any problems (except the stale share ofcourse) when I rebooted the machines after a couple of months.

If no container using that volume is started, you will only see the metadata file opts.json, which holds the details to mount the remote share. Once a container using that volume is started, you should see the content in the _data subfolder.

I realize we’re 5 years in the future, but this unhelpful reply still pops-up at the top of Google and I feel like you don’t/didn’t realize that during something like catastrophic power loss, not all systems come back up at the same time and so might not have those NFS shares ready to be mounted. So while it’s great that you’ve never had the problem, I really hope you’ve had the kind of personal growth that helps you address the problem someone is having instead of just going ‘oh, funny, I never have that problem’ and waste all of our time 5 years in the future.

@eatspamanddie: Welcome to the forum!

I see you just registered a user account for this classy post :smiley:

Please let me know how a container with restart policy always and unless-stopped is affected when a named volume, like shown above, is used?

Another less elegant way to do this is to edit the systemd docker service config and add your required NFS mounts as entries for “After” and “Requires”.

Use systemctl edit docker.service (and don’t directly edit systemd files) to have this persist through updates.

See also here: docker-compose wait to launch until a host filesystem is mounted - Stack Overflow