Version: 4.12

Container Restart Helper

injectRestartHelper

The injectRestartHelper option expects a boolean which decides if DevSpace should inject a restart helper and override the ENTRYPOINT+CMD of the image in runtime during the build process which wraps the regular container start command and allows to restart the regular container start command without terminating/recreating the container.

How does it work?

The idea of the restart helper is to start the restart helper script as PID 1 inside the conainer. This script will then start the original container entrypoint which will run your application. If a restart is triggered, the restart helper will just restart the application again.

Automatic Restart On File Change

The restart helper per se does not do much except restarting your application if it has been killed. You can combine this config option with the post-sync command restartContainer: true which will effectively restart your application inside the container every time DevSpace syncs files into the container.

Default Value For injectRestartHelper

injectRestartHelper: false

Example: Inject Restart Helper

images:
backend:
image: john/appbackend
frontend:
image: dscr.io/${DEVSPACE_USERNAME}/appfrontend
injectRestartHelper: true

Trigger a Restart

You can manually trigger a restart through the restart helper by doing the following:

# 1. Get the PID of your applications process
PID=$(cat /devspace-pid)
# 2. Remove the PID file to tell the restart helper that this is an intended restart
# (otherwise the restart helper will terminate as well which will kill the container)
rm /devspace-pid
# 3. Kill the application process (restart helper on PID 1 will detect this and restart it again)
kill -9 $PID
Automatic Restart On File Change

This is exactly what DevSpace is doing when the post-sync command restartContainer: true is configured.


Restart Helper Script

#!/bin/sh
#
# A process wrapper script to simulate a container restart. This file was injected with devspace during the build process
#
set -e
pid=""
trap quit TERM INT
quit() {
if [ -n "$pid" ]; then
kill $pid
fi
}
while true; do
setsid "$@" &
pid=$!
echo "$pid" > /devspace-pid
set +e
wait $pid
exit_code=$?
if [ -f /devspace-pid ]; then
# if the sync is currently active we try to restart instead of exiting
if [ -f /tmp/sync ]; then
rm -f /devspace-pid
printf "\nContainer exited with $exit_code. Will restart in 7 seconds...\n"
sleep 7
else
exit $exit_code
fi
fi
set -e
printf "\n\n############### Restart container ###############\n\n"
done