Docker: Scheduled Automatic Shutdown

For home servers and test environments, resource saving is important: containers can be stopped and system powered off during nights or off-hours. This guide describes a safe method with state preservation and automatic recovery. 💡 Method suits OrangePI, Raspberry Pi, old PCs, and any systems where power efficiency matters. 📦 Container shutdown script Create script # File: /usr/local/bin/stop_containers_and_shutdown.sh cat > /usr/local/bin/stop_containers_and_shutdown.sh << 'EOF' #!/bin/bash # # Script saves running container IDs, # stops them, and initiates system shutdown. # CONTAINERS_FILE="/etc/active_containers.txt" echo "=== $(date '+%Y-%m-%d %H:%M:%S') ===" echo "Starting container shutdown and system power-off script" # Get list of running containers (by ID) RUNNING_CONTAINERS=$(docker ps -q) if [ -n "${RUNNING_CONTAINERS}" ]; then echo "Saving running containers to ${CONTAINERS_FILE}" echo "${RUNNING_CONTAINERS}" > "${CONTAINERS_FILE}" docker stop ${RUNNING_CONTAINERS} echo "Containers stopped." else echo "No running containers." [ -f "${CONTAINERS_FILE}" ] && rm -f "${CONTAINERS_FILE}" fi sleep 10 echo "Shutting down system." /sbin/shutdown -h now EOF Make executable chmod +x /usr/local/bin/stop_containers_and_shutdown.sh How it works Step Description docker ps -q Gets IDs of all running containers > /etc/active_containers.txt Saves list for recovery docker stop Gracefully stops containers (SIGTERM) sleep 10 Allows time for operations to complete shutdown -h now Powers off the system Why this way: ...

17 Mar 2026 · 4 min · 848 words · Potato Energy Team, ponfertato