brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: give the Portainer Agent an AGENT_SECRET, so a rebuilt Portainer can still connect

Portainer
~/tips/tip-portainer-agent-secret

docker run -d -p 9001:9001 --name portainer_agent --restart=always \

Lets a Portainer agent accept any Portainer server that knows a shared secret, instead of only the first.

The Portainer Agent has a trust model that is easy to miss. When an agent starts, it waits for a Portainer server to claim it, and once that happens it only talks to that one instance. Every other Portainer that knocks gets rejected. The agent also gives up and shuts down if nobody claims it within five minutes of its first start.

That’s a sensible default, but it means a fresh Portainer server is a stranger to your existing agents. The optional AGENT_SECRET environment variable changes the rule: an agent with a secret accepts any Portainer instance that carries the same value. It has to be set on both sides when the containers start. On the server you add -e AGENT_SECRET=... to the Portainer container, and on each agent the same thing:

docker run -d -p 9001:9001 --name portainer_agent --restart=always \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /var/lib/docker/volumes:/var/lib/docker/volumes \
  -e AGENT_SECRET=yoursecret \
  portainer/agent:latest

For the value I’d take something long and random, for example the output of openssl rand -base64 32, and keep it in the same place as your other secrets.

The caveat cuts both ways. Portainer’s FAQ on agents that stop working after an upgrade boils down to this variable: if the server has a secret and an agent doesn’t, or the values differ, they won’t connect. So it’s all or nothing per server. And because the secret now decides who may manage the host, anyone who has it plus network access to port 9001 can point their own Portainer at your Docker socket. My advice is to treat it like a root password, and still keep port 9001 off the internet.

Further reading: Portainer FAQ: How does Portainer secure connectivity to Agents?, Portainer FAQ: Why have my agents stopped working after upgrading?

More Portainer tips Choose Polling in a Portainer GitOps update when your Git host can't reach Portainer Put Portainer environments in groups and give users access to a whole group Portainer's Edge Agent checks in every 5 seconds, and you can change that Deploy the container → Replace Public IP: 192.168.1.20 All Portainer tips (28) All topics

Link