Enable container auto restart
  • 1 Minute to read
  • Contributors
  • Dark
    Light
  • PDF

Enable container auto restart

  • Dark
    Light
  • PDF

Most of the time the servers on which the DNIF is hosted gets rebooted unmannerly like power failure or server read-only mode, so the docker container will be stopped during this period, and even after the server is started the container remains in the downstate.
We have to manually start the container. To resolve the need for manual intervention in this state, we have come with a solution of DNIF auto restart script which will start the container once the server is up and running.

Procedure

Step 1:

  • Create a file naming “dnif” in the path “/etc/init.d/” using the below command:
$ vim /etc/init.d/dnif

Step 2:

  • Insert the below lines into the file “dnif”:
#!/bin/bash
#chkconfig: 3 80 95
#processname: dnif	
# Source function library.
#. /etc/init.d/functions
start(){
sysctl -w vm.max_map_count=262144	
service docker start
#iptables -F			
/bin/docker-compose -f /data/dnif/docker-compose.yml down  #>change path accordingly
/bin/docker-compose -f /data/dnif/docker-compose.yml up -d
}
start

Step 3:

  • Check the docker-compose command path using the below command:
$ which docker-compose
  • Replace the highlighted section in the below lines of the “dnif” file appropriately according to the path given by the above command:
/bin/docker-compose -f /path/to/compose/file/docker-compose.yml down
/bin/docker-compose -f path/to/compose/file/docker-compose.yml up -d	
  • Save the File.

Step 4:

  • Change the dnif file permissions using the below command:
$ chmod 755 /etc/init.d/dnif

Step 5:

  • Edit/Create the rc.local file in the path /etc using the below command:
$ vim /etc/rc.local

Step 6:

  • Add the below lines in the rc.local file:
#!/bin/bash
/etc/init.d/dnif start
exit 0
  • Save the file.

Step 7:

  • Give executable permission to the rc.local file using the below command:
$ chmod +x /etc/rc.local

Step 8:

  • Enabling the rc-local service:

  • First create the file rc-local.service in the path /etc/systemd/system using below command:

$ vim /etc/systemd/system/rc-local.service
  • Paste the below contents in this file:
[Unit]
 Description=/etc/rc.local Compatibility
 ConditionPathExists=/etc/rc.local
				
[Service]
 Type=forking
 ExecStart=/etc/rc.local start
 TimeoutSec=0
 StandardOutput=tty
 RemainAfterExit=yes
 SysVStartPriority=99
			
[Install]
 WantedBy=multi-user.target
  • Save the file.

Step 9:

  • Now enable the rc-local service using below command:
$ systemctl enable rc-local

Step 10:

  • Enable the dnif service using the below command:
$ update-rc.d dnif defaults 80
Note

Please take a container image backup and add the new image name in the docker-compose.yml file.


Was this article helpful?