How to Automatically Turn Off Docker Containers On Unraid
文章介绍了一种通过Unraid的User Scripts插件自动启动和停止Docker容器的方法,以节省资源并减少后台进程。用户可以根据时间设置容器的启停时间,并通过cron表达式进行自定义调整。 2026-1-12 16:40:20 Author: www.blackmoreops.com(查看原文) 阅读量:0 收藏

I run quite a few Docker containers on my Unraid homelab, and some I don’t need 24/7. Some containers like development tools and media utilities don’t need to stay active overnight. I wanted to automatically turn off Docker containers on Unraid during off-hours to save resources and reduce unnecessary background processes.

Automatically turn off docker containers on Unraid using scheduled scripts

Automated Docker container scheduling on Unraid server using User Scripts plugin

After testing various approaches, I found a simple solution using the User Scripts plugin that handles both stopping and starting containers based on time of day. If you’re new to running Docker containers on Unraid, check out my complete guide on how to install Unraid NAS to get your server up and running first.

What You’ll Need

Before you can automatically turn on or off Docker containers, install these two plugins from the Unraid Community Applications:

  1. User Scripts – A plugin to act as a simple front end for any user scripts to allow you to run them without entering the command line
  2. User Scripts Enhanced – Adds categories, UI customisation, and toggleable elements to the User Scripts page

Both plugins are available in Apps → search for “User Scripts” and install them by squid. Over the years the maintainers have changed though, but the names of the scripts remained the same.

The Scheduling Script

Here’s the complete script I created to automatically turn on Docker containers on Unraid at 10 AM and stop them at 2 AM:

#!/bin/bash
# Auto start/stop containers based on time

HOUR=$(date +%H)

if [ "$HOUR" -eq 2 ]; then
    docker stop code-server obsidian pgadmin4 phpmyadmin lanspeedtest krusader
    echo "$(date): Containers stopped"
elif [ "$HOUR" -eq 10 ]; then
    docker start code-server obsidian pgadmin4 phpmyadmin lanspeedtest krusader
    echo "$(date): Containers started"
fi

Replace the container names with your own. In my case, I’m scheduling development and admin tools that I only use during working hours. For inspiration on which containers might benefit from scheduling, see my guide on the top self-hosted Docker apps for Unraid server.

If you need to figure out exact container names, you can either do it via GUI or run this command:

docker ps -a --format "table {{.Names}}\t{{.Image}}"

or

docker ps --format "table {{.Names}}\t{{.Status}}"

For example, if you’re running ytptube on Unraid for YouTube archiving, you might want to schedule it to run only during specific hours when you’re actively downloading content.

Setting Up the Schedule

  1. Open SettingsUser Scripts
  2. Click Add New Script
  3. Name your script: containers_auto_scheduler
  4. Click the script name to edit it
  5. Paste the script above (with your container names)
  6. Click Save Changes
  7. Create a category called Auto Schedule Containers, save it
  8. Add the containers_auto_scheduler script to this category, save it
  9. Click the gear icon next to your script
  10. Select ScheduleCustom
  11. Enter: 0 2,10 * * *
  12. Click Apply

The cron expression 0 2,10 * * * runs the script at 2:00 AM (stop) and 10:00 AM (start) every day.

Setting up automated container scheduling in Unraid User Scripts

Setting up automated container scheduling in Unraid User Scripts

Customising the Schedule

You can modify the timing by adjusting the cron schedule:

  • 0 1,9 * * * – Stop at 1 AM, start at 9 AM
  • 0 23,8 * * * – Stop at 11 PM, start at 8 AM
  • 0 2,10 * * 1-5 – Weekdays only (Monday to Friday)
  • 0 3,11 * * 6-7 – Weekends only (Saturday and Sunday)

This approach to automatically turn off Docker containers on Unraid has been running flawlessly on my homelab for months, saving resources without requiring manual intervention. If you’re also running monitoring tools like GoAccess, you can monitor your Nginx Proxy Manager with GoAccess on Unraid to track which containers are consuming the most resources.

Security Considerations

When scheduling containers, consider which services need to remain accessible. Critical infrastructure like authentication services should never be scheduled to stop. If you’re implementing single sign-on across your containers, my guide on setting up Authelia SSO on Unraid with Nginx Proxy Manager explains how to protect your services whilst maintaining 24/7 availability for essential systems.

For more advanced container management, you can also reference the official Unraid Docker documentation and the User Scripts plugin documentation.


文章来源: https://www.blackmoreops.com/automatically-turn-off-docker-containers-on-unraid/
如有侵权请联系:admin#unsafe.sh