π€ SleeperStack
SleeperStack is a lightweight automation container that safely starts or stops Docker containers managed via Portainer, using labels.
It's designed to work especially well with Portainer Community Edition, offering full functionality even without access to advanced stack metadata.
β¨ Features
- β
Stop or start containers (not stacks) based on custom labels
- β
100% compatible with Portainer Community Edition
- β
Works with containers deployed via Docker Compose stacks
- β
Supports multiple label values for grouping (e.g.
night, weekend)
- β
Reads container state: avoids redundant stop/start
- β
Optional
--dry-run mode to simulate actions
- β
Designed for safe automation: non-destructive and reversible
- β
Easily integrates with cron, Portainer Scheduled Jobs, or CI
π‘ Why Community-Friendly?
Portainer Community Edition does not expose stack-level labels or tags via API.
To ensure compatibility:
- SleeperStack works by reading labels applied to individual containers
- It uses
com.docker.compose.project to associate containers with their stack name
- It avoids stack deletion or privileged operations
- It uses only officially documented API endpoints available to API key users
This makes it safe and accessible for all setups β even without Portainer Business features.
π Usage
π§ Required Environment Variables
| Variable |
Description |
PORTAINER_URL |
Portainer API base URL (e.g. http://localhost:9000/api) |
PORTAINER_API_KEY |
API key from your Portainer user account |
PORTAINER_ENDPOINT_ID |
ID of the target Docker environment |
TARGET_LABEL |
Label to match containers, e.g. autoshutdown=night or autoshutdown=night,weekend |
ACTION |
Either stop or start |
DRY_RUN |
(Optional) Set to true to simulate actions without executing |
π§ͺ Example (.env + local)
PORTAINER_URL=http://dockerlab.local:9002/api
PORTAINER_API_KEY=your_api_key
PORTAINER_ENDPOINT_ID=2
ACTION=stop
TARGET_LABEL=autoshutdown=night,weekend
DRY_RUN=true
Then run:
python3 main.py
π¦ Docker Image Availability
SleeperStack is available on:
You can pull it with:
docker pull ghcr.io/gioxx/sleeperstack:latest
π³ Docker run example
docker run --rm --env-file .env gfsolone/sleeperstack
π·οΈ How to label your containers
In your docker-compose.yml, add:
services:
myapp:
image: my/image
labels:
autoshutdown: "night"
You can group containers using different values:
autoshutdown: "night"
autoshutdown: "weekend"
autoshutdown: "testlab"
Then run:
TARGET_LABEL=autoshutdown=night,weekend ACTION=stop python3 main.py
π¦ Dry Run Mode
To preview what would happen:
DRY_RUN=true ACTION=stop python3 main.py
No containers will actually be stopped or started.
π
Scheduling
You can run this tool via:
- Portainer Scheduled Jobs
- Host-level
cron
- CI/CD systems
Set different TARGET_LABEL and ACTION values to automate multiple groups.
π§ Notes
- Only containers with the target label are affected.
- Stack data is untouched β no stack removal or modifications.
- Uses
?all=true to support stopped container discovery.
- Designed for compatibility with Portainer Community and API Key access.
π Example crontab schedules
You can automate SleeperStack with multiple cronjobs to control different groups of containers based on their labels.
π Turn off test containers at night (22:00)
0 22 * * * docker run --rm -e PORTAINER_URL=... -e PORTAINER_API_KEY=... -e PORTAINER_ENDPOINT_ID=2 -e TARGET_LABEL=autoshutdown=night -e ACTION=stop gfsolone/sleeperstack
π
Turn them back on in the morning (07:00)
0 7 * * * docker run --rm -e PORTAINER_URL=... -e PORTAINER_API_KEY=... -e PORTAINER_ENDPOINT_ID=2 -e TARGET_LABEL=autoshutdown=night -e ACTION=start gfsolone/sleeperstack
π§ͺ Stop weekend-only environments (Friday 19:00)
0 19 * * 5 docker run --rm -e PORTAINER_URL=... -e PORTAINER_API_KEY=... -e PORTAINER_ENDPOINT_ID=2 -e TARGET_LABEL=autoshutdown=weekend -e ACTION=stop gfsolone/sleeperstack
ποΈ Restart them on Monday morning (08:30)
30 8 * * 1 docker run --rm -e PORTAINER_URL=... -e PORTAINER_API_KEY=... -e PORTAINER_ENDPOINT_ID=2 -e TARGET_LABEL=autoshutdown=weekend -e ACTION=start gfsolone/sleeperstack
π Tip: You can extract shared env vars into a .env file and reuse with --env-file.
π³ Docker Compose example
You can use docker-compose to schedule SleeperStack with different labels and time-based logic using cron syntax inside the container or with external tools like docker-cron.
π§ Basic example (manual run)
services:
sleeperstack-night:
image: gfsolone/sleeperstack
restart: "no"
environment:
PORTAINER_URL: http://dockerlab.local:9002/api
PORTAINER_API_KEY: your_api_key
PORTAINER_ENDPOINT_ID: "2"
TARGET_LABEL: autoshutdown=night
ACTION: stop
Run it manually:
docker-compose run --rm sleeperstack-night
β±οΈ With docker-cron
services:
sleeperstack-nightly:
image: gfsolone/sleeperstack
environment:
PORTAINER_URL: http://dockerlab.local:9002/api
PORTAINER_API_KEY: your_api_key
PORTAINER_ENDPOINT_ID: "2"
TARGET_LABEL: autoshutdown=night
ACTION: stop
cron:
image: aptible/docker-cron
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
CRON_TIME: "0 22 * * *"
CRON_COMMAND: "docker-compose run --rm sleeperstack-nightly"
π Note: docker-cron triggers commands in its environment, so keep docker.sock mounted and docker-compose installed if required.
π License
MIT β free to use, modify and distribute.
π€ Contributions
Pull requests welcome!
SleeperStack is built for responsible automation in all environments, including Community users.