Configuring NFS and Docker as a service

NFS before docker

Making sure NFS boots before docker.

This guide will make sure docker won't start before NFS is mounted, to prevent corrupted data, or unwated behaviors

Disable Docker autostart

First we will disable docker from autostarting on your sistem. Executing the follwing command

systemctl disable docker
systemctl disable docker

Checker Script

We will create a script on

vim /root/nfs-docker.sh
vim /root/nfs-docker.sh

This script will check NFS and will execute firewall script before booting docker.

#!/bin/bash
#You can edit where the mount path of the folder.
#Please make sure this folder is empty on your sistem, before mounting NFS
NFSCheck=/media/emsites/
#Set this on 1, if you want to execute firewall script
BOOTFIREWALL="0" # 1 = true
FIREWALLSCRIPT=/root/firewall.sh
LOG(){
        date=`date`;
        echo "$date - $1"
        echo "$date - $1" >> /tmp/docker-start.log;
}

MountNFS() {
        if [ $NFSBOOTED -eq 0 ] && [ $NFSMOUNT -eq 0 ]; then
                LOG "Trying to mount nfs"
                mount -a
                NFSBOOTED="1"
        fi
}
BootFirewall() {
        if [ $BOOTFIREWALL -eq 1]; then
                LOG "Booting firewall"
          				eval "$FIREWALLSCRIPT"
        fi
}

echo ""> /tmp/docker-start.log
LOG "Checking NFS mount"

FILES="0"
NFSBOOTED="0"

while [ $FILES -lt 1 ]
do
        NFSMOUNT=`df -h | grep $NFSCheck | wc -l`
        FILES=`ls $NFSCheck | wc | awk '{print $1}'`
        if [ $FILES -gt 0 ] && [ $NFSMOUNT -gt 0 ]; then
                LOG "NFS mounted";
                LOG "Booting Docker";
                systemctl start docker;
                LOG "Docker started, Please check systemctl status docker";
        else
                LOG "NFS NOT mounted";
                MountNFS;
        fi
        sleep 1;
done
#!/bin/bash
#You can edit where the mount path of the folder.
#Please make sure this folder is empty on your sistem, before mounting NFS
NFSCheck=/media/emsites/
#Set this on 1, if you want to execute firewall script
BOOTFIREWALL="0" # 1 = true
FIREWALLSCRIPT=/root/firewall.sh
LOG(){
        date=`date`;
        echo "$date - $1"
        echo "$date - $1" >> /tmp/docker-start.log;
}

MountNFS() {
        if [ $NFSBOOTED -eq 0 ] && [ $NFSMOUNT -eq 0 ]; then
                LOG "Trying to mount nfs"
                mount -a
                NFSBOOTED="1"
        fi
}
BootFirewall() {
        if [ $BOOTFIREWALL -eq 1]; then
                LOG "Booting firewall"
          				eval "$FIREWALLSCRIPT"
        fi
}

echo ""> /tmp/docker-start.log
LOG "Checking NFS mount"

FILES="0"
NFSBOOTED="0"

while [ $FILES -lt 1 ]
do
        NFSMOUNT=`df -h | grep $NFSCheck | wc -l`
        FILES=`ls $NFSCheck | wc | awk '{print $1}'`
        if [ $FILES -gt 0 ] && [ $NFSMOUNT -gt 0 ]; then
                LOG "NFS mounted";
                LOG "Booting Docker";
                systemctl start docker;
                LOG "Docker started, Please check systemctl status docker";
        else
                LOG "NFS NOT mounted";
                MountNFS;
        fi
        sleep 1;
done

Create Unit for systemctl

Creating a Unit service in systemd for your script
Create File:

vim cat /etc/systemd/system/nfs-docker.service
vim cat /etc/systemd/system/nfs-docker.service

Copy content

[Unit]
Description=Launch NFS and Docker

[Service]
Type=simple
#ExecStart=
ExecStart=/root/nfs-docker.sh

[Install]
WantedBy=multi-user.target
[Unit]
Description=Launch NFS and Docker

[Service]
Type=simple
#ExecStart=
ExecStart=/root/nfs-docker.sh

[Install]
WantedBy=multi-user.target

enable script to autostart

Enabling script on startup

systemctl enable nfs-docker
systemctl enable nfs-docker