This is a modification to @n8jadams' post, in which instead of checking the date every second, it will calculate the number of seconds needed to sleep, rather than checking every minute if it is the correct minute. This will improve the accuracy, and have the container restart at the specified time.
version: "3"
services:
myservice:
container_name: myservice
restarter:
image: docker:cli
restart: unless-stopped
volumes: ["/var/run/docker.sock:/var/run/docker.sock"]
entrypoint: ["/bin/sh","-c"]
command:
- |
while true; do
current_epoch=$$(date +%s)
target_epoch=$$(( $$(date -d "05:00" +%s) + 86400 ))
sleep_seconds=$$(( target_epoch - current_epoch ))
echo "$$(date) + $$sleep_seconds seconds"
sleep $$sleep_seconds
docker restart myservice
done
Note: the echo in the script will allow you to copy/paste into the date command, to see the specified time it will restart (I couldn't seeem to get it to correctly add the time with the date command, provided by docker:cli), eg. date -d "$echo from above"