Last active
April 14, 2026 06:08
-
-
Save anthonycoffey/e2672f77baaa1b561d4f7954301d7a33 to your computer and use it in GitHub Desktop.
kill/restart "stuck" docker after crash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # ================================================================= | |
| # Docker Desktop Recovery Script for macOS | |
| # Use this when Docker Desktop hangs or fails to start due to | |
| # stale socket files or "out of disk space" crashes. | |
| # ================================================================= | |
| echo "🚀 Starting Docker Desktop recovery..." | |
| # 1. Forcefully terminate all Docker-related backend processes | |
| # We use pkill to find processes by name. 2>/dev/null hides errors if no process is found. | |
| echo "Stopping Docker processes..." | |
| pkill -9 -f "com.docker.backend" 2>/dev/null | |
| pkill -9 -f "docker info" 2>/dev/null | |
| pkill -9 -f "docker-ai" 2>/dev/null | |
| # Give the OS a moment to release the process hooks | |
| sleep 2 | |
| # 2. Identify any stragglers | |
| # This lists remaining Docker processes while filtering out the grep command itself | |
| # and specific network daemons that shouldn't be killed. | |
| echo "Checking for remaining Docker processes..." | |
| ps aux | grep -i docker | grep -v grep | grep -v vmnetd | grep -v "pkill" | |
| # 3. Clean up stale communication files | |
| # Docker uses .sock, .lock, and .pid files to manage its state. | |
| # If these remain after a crash, Docker Desktop will fail to launch. | |
| # We use /Users/$USER to make this script work for any macOS user. | |
| RUN_DIR="/Users/$USER/.docker/run" | |
| echo "Cleaning up stale socket and lock files in $RUN_DIR..." | |
| rm -f "$RUN_DIR/docker.sock" | |
| rm -f "$RUN_DIR"/*.lock 2>/dev/null | |
| rm -f "$RUN_DIR"/*.pid 2>/dev/null | |
| # 4. Verify cleanup | |
| if [ -d "$RUN_DIR" ] && [ "$(ls -A "$RUN_DIR" 2>/dev/null)" ]; then | |
| echo "⚠️ Note: Some files remain in $RUN_DIR:" | |
| ls "$RUN_DIR" | |
| else | |
| echo "✅ Run directory is clear." | |
| fi | |
| # 5. Relaunch Docker Desktop | |
| # This triggers the standard macOS 'open' command for the application. | |
| echo "Restarting Docker Desktop..." | |
| open -a Docker && echo "✨ Docker Desktop launch requested." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment