Setting up Portainer and Portainer Agent with Docker Compose simplifies container management, enabling efficient multi-host monitoring, centralized control, and enhanced team collaboration through a user-friendly web interface for Docker and Kubernetes environments.
My words on Portainer vs Docker Desktop
Portainer offers a web-based user interface that simplifies the management of Docker environments and Kubernetes clusters, making it an attractive option over Docker Desktop for teams and organizations seeking more extensive container management capabilities. It excels in multi-user access control, providing detailed user permissions and roles, which is crucial for team collaboration and security in larger environments. Portainer's ability to manage multiple Docker hosts and Kubernetes clusters from a single dashboard enhances operational efficiency, offering a centralized view that Docker Desktop lacks. This makes it especially beneficial for businesses scaling their containerized applications across various environments or requiring detailed oversight and governance of their container infrastructure.
That introduction might lead you to believe this is a Docker Windows-ish post, but it is not. I just put the Docker Desktop information there to lure people in, this should work in most environments.
Considerations
- You are most likely a developer or IT pro wanting a more standardized way to deploy Portainer and Portainer Agent.
- You may also have read other blog posts on this topic realizing they are all bad and misleading.
- You want to set up Portainer Agent to offer easy access to the very same Portainer environment you are deploying.
- You do NOT want to access the local Portainer environment you are deploying using the Agent.
- You may want to access Portainer locally but also want to connect to it from another Portainer installation using Portainer Environments.
Other thoughts
I have added delay and window to the restart_policy options, this may not be available in all setups, they do however not hurt to have there if yours aren't compatible.
Many guides recommend you to put a command in your Portainer config having it execute
command: -H tcp://portainer-agent:9001 --tlsskipverify
This makes absolutely no sense at all since that would use your agent to administer the local docker environment. Instead, run without it and if you are running Portainer through localhost connect to the local environment.
This approach has also made me set the depend condition on the Agent to be dependent on the Portainer instance and not the other way around.
Deploying
I've added the following config to portainer-compose.yml.
docker-compose -f .\portainer-compose.yml up --detach
The compose file
version: '3.9' name: portainer services: portainer-agent: container_name: portainer-agent image: portainer/agent ports: - "9001:9001" volumes: # Mount the host's Docker socket into the container - /var/run/docker.sock:/var/run/docker.sock # Mount the host's Docker volumes into the container - /var/lib/docker/volumes:/var/lib/docker/volumes depends_on: - portainer deploy: resources: limits: cpus: '0.5' memory: 1024M restart_policy: condition: unless-stopped delay: 5s window: 120s portainer: container_name: portainer image: portainer/portainer-ce:latest ports: - "8000:8000" - "9443:9443" volumes: # Mount the host's Docker socket into the container - /var/run/docker.sock:/var/run/docker.sock # Create a named volume for persistent Portainer data storage - portainer_data:/data networks: - portainer_network deploy: resources: limits: cpus: '0.5' memory: 1024M restart_policy: condition: unless-stopped delay: 5s window: 120s networks: portainer_network: driver: bridge volumes: portainer_data: name: portainer_data
Login to Portainer via https://localhost:9443 or https://yourhostname:9443 and setup passwords etc.
You should now have Portainer and Portainer in the same stack.
Enjoy.