Files
big-bear-scripts/reset-portainer-password/README.md
Christopher 31aaabc368 feat: Add Portainer password reset script (#45)
*  feat: Add Portainer password reset script

This commit introduces a new script `run.sh` that allows users to easily reset the Portainer administrator password. The script performs the following key changes:

- Checks if Docker is running and accessible
- Finds the Portainer container and its associated data volume
- Detects the deployment type (container, service, or stack)
- Stops the Portainer instance based on the deployment type
- Provides a user prompt to confirm the password reset
- Resets the Portainer administrator password

The script is designed to be user-friendly, providing clear instructions and error handling to ensure a smooth password reset process. This feature will help Portainer users who have lost or forgotten their administrator password, allowing them to regain access to their Portainer instance.

* 🔧 feat(reset-portainer-password): Add support for Portainer stacks

This commit adds support for resetting the Portainer administrator password
when Portainer is deployed as a Docker stack. Previously, the script only
supported Portainer containers and services.

The key changes are:

- Detect if Portainer is deployed as a stack and find the corresponding
  service name
- Scale down the Portainer stack service to 0 replicas before resetting the
  password
- Scale up the Portainer stack service to 1 replica after resetting the
  password
- Improve the logic for finding the Portainer data volume or bind mount
- Update the README with information about bind mounts

* 🔧 refactor: Improve error handling in Portainer password reset script

The changes in this commit focus on improving the error handling in the Portainer password reset script. The main changes are:

- Replaced the `check_command` function with a more generic `handle_error` function that prints the error message and exits the script.
- Simplified the error handling in various places by using the `|| handle_error` pattern instead of explicit error checking.
- Temporarily disabled `set -e` when running the Portainer password reset helper to properly capture the exit code.
- Improved the logic for finding the Portainer container and volume/mount by using the `||` operator instead of explicit error checking.

These changes make the script more robust and easier to maintain by centralizing the error handling logic and reducing duplication.

*  refactor: improve error handling and rename error function

Replace generic handle_error calls with handle_command_error for clearer
command failure handling in Portainer service scaling and container
operations. Introduce a detailed handle_error function to trap errors,
providing line number, exit code, and failed command for better diagnostics.
Enhance script robustness and maintainability by improving error reporting.

* feat(reset-portainer-password): improve error handling and deployment detection

Enhance error handling with detailed diagnostics on failure using ERR trap.
Refine Docker and Portainer container checks for accuracy and robustness.
Improve volume detection logic to handle mounts and fallback volume names.
Add deployment type detection to distinguish container, service, or stack.
Update script flow to confirm actions and handle different deployment types
appropriately when stopping and restarting Portainer.
2025-06-18 11:27:23 -05:00

3.5 KiB

Reset Portainer Password

This script helps you easily reset the administrator password for your Portainer instance using the official Portainer helper-reset-password container.

Features

  • Automatic Detection: Automatically finds your Portainer container and data volume
  • Multiple Deployment Support: Works with containers, services, and stacks
  • Safe Operation: Properly stops and restarts Portainer during the reset process
  • Clear Output: Displays the new password clearly for easy copying
  • Error Handling: Comprehensive error checking and recovery

Prerequisites

  • Docker must be installed and running
  • Portainer must be installed with a persistent data volume
  • Root or sudo access may be required

Usage

Run the script directly from GitHub:

bash -c "$(wget -qLO - https://raw.githubusercontent.com/bigbeartechworld/big-bear-scripts/master/reset-portainer-password/run.sh)"

Or with curl:

bash -c "$(curl -fsSL https://raw.githubusercontent.com/bigbeartechworld/big-bear-scripts/master/reset-portainer-password/run.sh)"

What the Script Does

  1. Checks Docker Status: Verifies Docker is running and accessible
  2. Finds Portainer: Automatically locates your Portainer container
  3. Detects Data Volume: Identifies the Portainer data volume
  4. Determines Deployment Type: Detects if running as container, service, or stack
  5. Stops Portainer: Safely stops the Portainer instance
  6. Resets Password: Uses the official Portainer helper to reset the admin password
  7. Restarts Portainer: Brings Portainer back online
  8. Displays New Password: Shows the new randomly generated password

Supported Deployments

  • Standard Container: docker run deployments
  • Docker Services: docker service deployments
  • Docker Stacks: Stack-based deployments

Important Notes

  • ⚠️ This will temporarily stop your Portainer instance during the reset process
  • 🔐 Save the new password immediately - it's randomly generated and cannot be recovered
  • 👤 Resets the original admin account (UserID == 1) - if removed, creates a new admin user
  • 📝 Works with persistent storage - both named Docker volumes and bind mounts are supported

Troubleshooting

Container Not Found

If the script can't find your Portainer container, ensure:

  • Portainer is installed and the container exists
  • The container name contains "portainer"

Volume or Bind Mount Not Found

If the data storage can't be located:

  • Verify Portainer has persistent storage mounted to /data (either a named volume or bind mount)
  • For named volumes: Check that the volume exists with docker volume ls
  • For bind mounts: Ensure the host directory exists and is accessible

Permission Errors

If you encounter permission errors:

  • Run the script with sudo
  • Ensure your user is in the docker group

Security

This script uses the official Portainer helper container (portainer/helper-reset-password) which:

  • Only resets the password for the original administrator account
  • Generates a secure random password
  • Does not expose or store credentials

Support

If you find this script helpful, please consider supporting BigBearTechWorld: