A PowerShell-based utility for gracefully shutting down Docker containers with dependency awareness and visual progress tracking.
@echo off
setlocal EnableDelayedExpansion
color 0B
...
This is the launcher script that:
- Provides a user-friendly interface
- Checks for required files and permissions
- Executes the PowerShell script
- Displays results
# Function to show progress bar
function Show-Progress {
param (
[string]$containerName,
[string]$status
)
...
}
This is the main PowerShell script that handles:
- Container dependency detection
- Shutdown order calculation
- Progress visualization
- Graceful shutdown logic
function Show-Progress {
# Shows a progress bar like:
# [##########] 50% - container_name : Shutting down
}
Creates a visual progress indicator for each container being shut down.
function Get-ContainerDependencies {
# Detects dependencies through:
# 1. Network mode dependencies (--network container:name)
# 2. Container links (--link)
# 3. Docker Compose dependencies (depends_on)
# 4. Shared networks (containers in same non-default network)
}
The dependency detection system now includes:
- Network-based dependency detection for containers sharing custom networks
- Multi-network support for containers connected to multiple networks
- Proper handling of Docker Compose relationships
- Circular dependency detection and resolution
function Get-ShutdownOrder {
# Uses topological sorting to determine
# safe shutdown order
}
Calculates the correct order to shut down containers without breaking dependencies.
try {
# 1. Get dependencies
# 2. Calculate order
# 3. Show progress
# 4. Shut down containers
}
Orchestrates the entire shutdown process.
- Place both files in the same directory
- Run
shutdownallcontainers.cmd
- Watch the progress as containers are gracefully stopped
+------------------------------------------------+
| Docker Graceful Shutdown Tool |
+------------------------------------------------+
Processing container: container1
[####################] 100% - container1 : Stopped
- Windows OS
- PowerShell 5.1+
- Docker Desktop
- Admin rights (recommended)
- Keep both files together
- Run as administrator
- Default timeout is 30 seconds per container
- Failed graceful shutdowns will attempt force stop
The script handles:
- Missing files
- Permission issues
- Failed shutdowns
- Circular dependencies
For issues or questions:
- Check file permissions
- Verify Docker is running
- Run as administrator
- Check for circular dependencies
We welcome contributions! Please see our Contributing Guidelines for details on:
- How to submit changes
- How to report issues
- Coding standards
- Development process
- Windows 10/11
- PowerShell 5.1 or higher
- Docker Desktop for Windows
- Git
- Your favorite code editor (VS Code recommended)
-
Clone the repository
git clone https://github.com/PeterVinter/docker-graceful-shutdown-for-windows.git cd docker-graceful-shutdown-for-windows
-
Set up PowerShell execution policy (if needed)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
-
Test the scripts
shutdownallcontainers.cmd -test
- Use clear, descriptive variable names
- Add comments for complex logic
- Follow PowerShell Best Practices
Before submitting changes:
- Run the script with
-test
parameter - Test with various container configurations
- Verify proper error handling
- Check script performance
The project includes comprehensive test coverage using Pester framework and GitHub Actions:
- Automated testing on Windows environments
- Safe and isolated test execution
- Extensive test scenarios
For detailed testing information, see tests/README.md.
The /examples
directory contains several scenarios demonstrating the utility's capabilities:
-
Basic Web Stack (examples/basic-web)
- Simple web application with database
- Basic network dependencies
-
Microservices Architecture (examples/microservices-example)
- Multi-layer application architecture
- Complex network topology
- See directory README for details
The utility includes comprehensive logging functionality:
- File-based logging in
./logs
- Console output with color-coding
- Multiple log levels (INFO, WARNING, ERROR, DEBUG)
Enable verbose logging:
$VerbosePreference = 'Continue'
.\gracefully_shutdown_all_docker_containers.ps1
For more details about logging configuration, see modules/Logging.psm1.
MIT License - Feel free to modify and distribute