Docker Community Forums

Share and learn in the Docker community.

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.