Skip to content

Instantly share code, notes, and snippets.

@peter279k
Last active April 24, 2020 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter279k/608862895fa57862956cc0c55c88c9ff to your computer and use it in GitHub Desktop.
Save peter279k/608862895fa57862956cc0c55c88c9ff to your computer and use it in GitHub Desktop.
This is for creating restarted container as background service
#!/bin/bash
# References: https://dev.to/suntong/autostart-docker-container-with-systemd-5aod
# Docker container running command: docker run -dit --restart always my-docker-image-name
# Don't forget to use --restart always option when running above command!
function ask_reboot () {
read -p 'Do you want to reboot system? [Y/n] ' reboot_ans
if [[ $reboot_ans == 'Y' ]]; then
echo "Let's reboot system to verify this background service!"
${sudo_prefix}systemctl reboot
fi;
}
function remove_container_name () {
${sudo_prefix}docker rm ${container_name}
}
container_name=$1
uninstall=$2
if [[ ${container_name} == '' ]]; then
echo 'Please input container name...'
exit 1;
fi;
echo 'Check docker command is existed...'
which docker 2>&1 > /dev/null
if [ $? != 0 ]; then
echo 'docker command not found...'
echo 'Please install docker-ce package firstly...'
exit 1;
fi;
sudo_prefix=''
echo 'Check whether current user can use docker command directly...'
cat /etc/group | grep docker | grep ${USER} 2>&1 > /dev/null
if [ $? != 0 ]; then
sudo_prefix='sudo '
fi;
if [[ $uninstall == '--uninstall' ]]; then
echo 'Uninstall service starts....'
read -p "Do you want to uninstall and disable ${container_name}? [Y/n] " disable_ans
if [[ ${USER} != 'root' ]]; then
sudo_prefix='sudo '
fi;
if [[ $disable_ans == 'Y' ]]; then
echo "Check background ${container_name} service is running and its status...."
container_status=$(${sudo_prefix}systemctl is-enabled ${container_name}.service)
if [ ${container_status} == 'enabled' ]; then
echo "The background ${container_name} service is active...."
echo 'Stopping this service....'
${sudo_prefix}systemctl stop ${container_name}
echo 'Disable this service....'
${sudo_prefix}systemctl disable ${container_name}
echo "Remove ${container_name} service config file...."
${sudo_prefix}rm -f /etc/systemd/system/${container_name}.service
remove_container_name
ask_reboot
exit 0;
fi;
else
echo 'Stopped it.'
exit 0;
fi;
fi;
echo 'Check specific container is running....'
${sudo_prefix}docker ps | grep ${container_name} 2>&1 > /dev/null
if [ $? != 0 ]; then
echo "The specific container is not found on Docker process..."
echo "Please start this ${container_name} firstly!"
exit 1;
fi;
echo 'Creating Docker container service file for systemd....'
if [ -f /etc/systemd/system/${container_name}.service ]; then
echo "It seems that the ${container_name}.service is existed."
read -p 'Do you want to overwrite? [Y/n] ' overwrite_ans
if [ $overwrite_ans == 'Y' ]; then
echo "Let's create service file!"
else
echo 'Stopped it...'
echo 'Please use another file name and restart this script...'
exit 0;
fi;
fi;
service_file_name="${container_name}.service"
echo '[Unit]' > ${service_file_name}
if [ $? != 0 ]; then
echo "Cannot write service file on ${PWD} folder. Maybe it's about permission denied?"
echo 'Stopped it.'
exit 1;
fi;
echo "Description=${container_name}" >> ${service_file_name}
echo "Requires=docker.service" >> ${service_file_name}
echo "After=docker.service" >> ${service_file_name}
echo '' >> ${service_file_name}
echo '[Service]' >> ${service_file_name}
echo 'Restart=always' >> ${service_file_name}
echo "ExecStart=/usr/bin/docker start -a ${container_name}" >> ${service_file_name}
echo "ExecStop=/usr/bin/docker stop -t 2 ${container_name}" >> ${service_file_name}
echo '' >> ${service_file_name}
echo "StandardError=/var/log/${container_name}.service.log" >> ${service_file_name}
echo '' >> ${service_file_name}
echo '[Install]' >> ${service_file_name}
echo 'WantedBy=default.target' >> ${service_file_name}
echo 'Let this service file move to /etc/systemd/system/ folder...'
echo "Check systemd can be used by this ${USER}...."
if [[ ${USER} != 'root' ]]; then
echo 'You are not root user and may input password for sudo command...'
sudo_prefix='sudo '
fi;
${sudo_prefix}mv ${service_file_name} /etc/systemd/system/${service_file_name}
echo "Let systemd daemon reload and register/enable the specific ${service_file_name}..."
${sudo_prefix}systemctl daemon-reload
${sudo_prefix}systemctl enable --now ${service_file_name}
${sudo_prefix}systemctl is-enabled ${service_file_name}
${sudo_prefix}systemctl start ${service_file_name}
ask_reboot
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment