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?