https://github.com/piero24/dockerrestartscript
Simple script for containers that do not restart properly
https://github.com/piero24/dockerrestartscript
big-bear-casaos casaos casaos-appstore docker docker-compose homeserver openvpn openvpn-monitor openvpn-server restart restart-container server
Last synced: 2 months ago
JSON representation
Simple script for containers that do not restart properly
- Host: GitHub
- URL: https://github.com/piero24/dockerrestartscript
- Owner: Piero24
- License: mit
- Created: 2024-08-03T14:36:14.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-08-03T14:59:55.000Z (9 months ago)
- Last Synced: 2025-02-23T04:27:26.108Z (2 months ago)
- Topics: big-bear-casaos, casaos, casaos-appstore, docker, docker-compose, homeserver, openvpn, openvpn-monitor, openvpn-server, restart, restart-container, server
- Language: Shell
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DockerRestartScript
This is a simple script for people who have had problems with Docker container auto-restart after a reboot or an incorrect shutdown. In my case, the problem is with the [OpenVPN Access Server](https://openvpn.net) Docker container.
Specifically, these applications are run on Docker containers managed by [CasaOS](https://www.casaos.io).## Problem Definition
After a restart caused by a power outage or other issues, all containers would function and start automatically except for the OpenVPN container.
Not finding an optimal solution, I decided to create a script that stops the failed auto-restart attempt of the container and starts a new, functioning one.## Basic Info
The script waits for the operating system and Docker containers to load. Then it checks if the OpenVPN container is loaded correctly. If it is not, the script stops the current container and starts a new one.
The script is modifiable and adaptable to other containers that may have similar issues. Simply change the name of the container you want to monitor.## Prepare the Script
Modify the script as follows:
- Change **CONTAINER_NAME** to the name of the container you want to monitor.
-
## Start the Script at Boot
To start the script at system startup, a couple of steps are enough.- Save the script to an appropriate location on your system, such as `/usr/local/bin/check_and_restart_container.sh`, and make it executable with the command sudo `chmod +x /usr/local/bin/check_and_restart_container.sh`
- Create a service file called mounter.service in /etc/systemd/system/: `sudo nano /etc/systemd/system/check_and_restart_container.service`
- Inside this file, insert the following content:```ini
[Unit]
Description=Check and Restart Docker Container if Restarting
After=network.target docker.service
Requires=docker.service[Service]
Type=oneshot
ExecStart=/usr/local/bin/check_and_restart_container.sh
RemainAfterExit=true[Install]
WantedBy=multi-user.target
```- restart the deamon `sudo systemctl daemon-reload`
- enable the service to start during boot: `sudo systemctl enable /etc/systemd/system/check_and_restart_container.service`Now, when the system starts, the script checks if the container is running properly.