Start docker after NFS mounts come online


Some of my docker containers access resources from a NAS over NFS.

On reboot, the Docker service comes up before the NFS paths are mounted, this causes the services within my containers to fail.

Restating the docker service, systemctl restart docker fixes this but I’d rather the containers come up properly the first time.


Add a systemd drop-in unit for the docker service instructing it to wait for the NFS mount to come online before starting.


  1. Identify the systemd unit for the NFS mount. In my case /nas/data .
[email protected]:~$ mount -t nfs4 on /nas/data type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=,local_lock=none,addr=,_netdev)  

[email protected]:~$ sudo systemctl list-units | grep nas-data
  nas-data.mount                                                                                                         loaded active mounted   /nas/data  
  1. Create the systemd drop-in unit file for the docker service. Create the directory if it is not there.
[email protected]:~$ sudo mkdir /etc/systemd/system/docker.service.d/  

[email protected]:~$ sudo echo "[Unit]
Wants=nas-data.mount" >> /etc/systemd/system/docker.service.d/wait-for-nfs.conf

[email protected]:~$ sudo cat /etc/systemd/system/docker.service.d/wait-for-nfs.conf 
[sudo] password for danesh: 

After: Start after NFS /nas/data is mounted.
Wants: If not already mounted, go ahead and mount /nas/data

If Wants is changed to Requires, the docker service will not start unless /nas/data is mounted.

  1. Restart the docker service to verify that the drop-in is picked up.
[email protected]:~$ sudo systemctl restart docker

[email protected]:~$ sudo systemctl status docker
? docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/docker.service.d
     Active: active (running) since Mon 2020-08-31 20:45:40 UTC; 5h 21min ago
TriggeredBy: ? docker.socket

If the drop-in is showing then the change will now be persistent across reboots.


Soichi Hayashi
Freedesktop Systemd