November 28, 20241 yr We'll all have it at some point, the dreaded batch of docker containers which refuse to stop - when you tell them to - due to processes running in the background. I wrote a small script for when it happens to myself and thought I would put it up here. WHAT THE SCRIPT DOES This script is designed to handle situations where Docker containers are unresponsive and cannot be stopped using standard Docker commands. The script is particularly useful for recovering from Docker issues where containers remain in a running or hung state due to malfunctioning containerd-shim processes. It avoids data loss by not removing or deleting containers, focusing only on safely stopping them after resolving the shim process issue. Specifically, it: Identifies Running Containers: Uses docker ps -q to list all currently running container IDs. Locates Associated containerd-shim Processes: For each running container, the script searches for its corresponding containerd-shim process, which manages the container's lifecycle. Kills the containerd-shim Processes: If a containerd-shim process is found for a container, it is forcefully terminated using kill -9. Attempts to Stop Containers: After killing the associated containerd-shim, the script runs docker stop to cleanly stop the container. Logs Actions: Outputs messages to the terminal detailing which containers are being processed, the shim processes being terminated, and the success or failure of container stop attempts. Ensures All Containers Are Processed: Loops through all running containers and handles them one by one. THE SCRIPT: #!/bin/bash # Get list of running container IDs running_containers=$(docker ps -q) # Loop through each container ID for container_id in $running_containers; do echo "Processing container: $container_id" # Find the corresponding shim process for the container shim_pid=$(ps aux | grep "containerd-shim-runc-v2" | grep "$container_id" | awk '{print $2}') if [ -n "$shim_pid" ]; then echo "Killing shim process $shim_pid for container $container_id" kill -9 $shim_pid else echo "No shim process found for container $container_id" fi # Attempt to stop the container echo "Stopping container $container_id" docker stop $container_id || echo "Failed to stop container $container_id" done echo "All running containers processed." Instructions to Use the Script on Unraid: Open the Unraid Terminal: Click on "Terminal" from the Unraid Web UI or SSH into your Unraid server. Create the Script File: In the terminal, create a new script file: "nano kill_shims_and_stop_containers.sh" Paste the Script: Copy the script from above and paste it into the terminal. To paste in the terminal, use Ctrl+Shift+V (or right-click and select Paste). Save and exit by pressing Ctrl+O, then Enter, and finally Ctrl+X. Make the Script Executable: Give the script execute permissions: "chmod +x kill_shims_and_stop_containers.sh" Run the Script: Execute the script to process running containers: "./kill_shims_and_stop_containers.sh" Monitor Output: The script will display which containers are being processed, which containerd-shim processes are terminated, and whether containers are successfully stopped. Verify the Containers Are Stopped: After the script completes, check the container status to ensure they are stopped: "docker ps" Edited November 28, 20241 yr by martinkeat
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.