🐳 Using PowerShell to Run Scripts When a Linux Docker Container Stops
Docker is now a staple for deploying containerized applications, and PowerShell has grown into a versatile, cross-platform automation solution. Although running PowerShell on Linux may seem unusual at first, it’s a standard practice in modern DevOps — thanks to the introduction of PowerShell Core (pwsh
).
This tutorial walks you through how to automatically trigger a PowerShell script when a Docker container stops on a Linux system — a technique commonly used for log handling, resource cleanup, sending alerts, or initiating CI/CD operations.
🔍 Why Execute Scripts on Container Stop?
Triggering actions when a container stops is helpful for:
-
🧹 Cleaning up temporary files or system resources
-
🗂️ Backing up logs or application data
-
📣 Sending alerts via email, webhook, or messaging services
-
🚀 Kicking off downstream workflows or shutdown routines
-
📋 Capturing audit information
In traditional scripting, Linux uses signals like SIGTERM
with trap
. Docker, however, offers more advanced event handling, and with PowerShell Core, it becomes even more powerful.
📦 Docker Container Lifecycle Basics
Docker containers move through these states:
-
Created
-
Running
-
Paused
-
Stopped
-
Dead
The key moment for script execution is during the transition from Running to Exited, which can be caused by:
-
Manual
docker stop
-
App crashing or exiting normally
-
Reaching resource limits (CPU, RAM)
-
Host system shutdown
⚙️ PowerShell on Linux
PowerShell Core is open-source and runs natively on Linux. It can be installed via apt
, yum
, or snap
, or included directly in Docker images.
Install Example (Ubuntu):
sudo apt update
sudo apt install -y powershell
Run PowerShell:
pwsh
Basic Script Example:
Write-Output "Container is stopping..."
# Add custom cleanup logic here
🧰 Prerequisites
To get started, you’ll need:
-
Docker installed on your Linux machine
-
PowerShell Core (
pwsh
) -
Basic knowledge of shell scripting
-
(Optional) Understanding of Docker event handling or entrypoints
🧭 Overview of Strategies
Method | Description | Best Use Case |
---|---|---|
Docker Events Listener | Monitors lifecycle events globally | Multiple containers |
Entrypoint Trap | Uses signal traps inside containers | Container-specific automation |
External Watcher | Monitors container status from outside | Legacy systems or custom logic |
Let’s explore each.
✅ Method 1: Docker Events Listener
This method uses Docker’s event stream to listen for container stop events.
Step 1: Create the PowerShell Script
# stop-action.ps1
Write-Output "Container stopped at $(Get-Date)"
# Insert your logic here
Step 2: Create a Bash Event Listener
#!/bin/bash
docker events --filter 'event=die' --format '{{json .}}' | while read event; do
echo "Detected stop: $event"
pwsh /opt/scripts/stop-action.ps1
done
Step 3: Run in Background
chmod +x /opt/scripts/docker-listener.sh
nohup /opt/scripts/docker-listener.sh &
✅ Method 2: Entrypoint Trap Inside Dockerfile
Use signal traps in a container’s entrypoint to run a script during shutdown.
Dockerfile Example
FROM ubuntu:22.04
RUN apt update && apt install -y wget apt-transport-https software-properties-common \
&& wget -q https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb \
&& dpkg -i packages-microsoft-prod.deb \
&& apt update && apt install -y powershell \
&& rm packages-microsoft-prod.deb
COPY cleanup.ps1 /opt/cleanup.ps1
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]