Add Portainer to Do...
 
Notifications
Clear all

[Solved] Add Portainer to Docker Compose

1 Posts
1 Users
0 Reactions
305 Views
Brandon Lee
Posts: 395
Admin
Topic starter
(@brandon-lee)
Member
Joined: 14 years ago

Portainer is a great tool that I use a lot in the home lab. You can use it to manage your docker hosts and install "stacks" in the Portainer world that equate to Docker Compose code that you can use to easily deploy your app stacks comprised of many different containers. What I like about Portainer is that you can create your own custom templates where you can use your own docker-compose code to deploy apps and stacks of apps.

However, you may wonder the process to add portainer to Docker Compose itself? In other words, how do you install Portainer using Docker compose?ย 

Well, actually fairly easily, you can do this with code like the following:

version: "3"

services:
  portainer:
    image: portainer/portainer-ce:latest
    container_name: portainer             
    restart: always                       
    ports:
      - "9000:9000"                       
      - "9443:9443"                      
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"  # Mount the Docker socket to allow management of Docker
      - "portainer_data:/data"             # Persistent volume for Portainer data

volumes:
  portainer_data:                          # Define the persistent volume for Portainer data

In the above:

  • We are pulling the community edition of Portainer which is free
  • We are setting the container nmae
  • we are setting the restart policy to always restart
  • We are defining the port configuration
    • Port 9000 is for agent communication
    • Port 9443 is for the Portainer UI
  • We are also setting the volumes configuration, mounting the Docker socket to allow management of Docker from Portainer
    • Then the persistent volume for portainer data
  • Then finally, the volume configuration is defined

If you want to use something like Traefik as a reverse proxy, your Docker compose code might look like the following (not showing the Traefik config here) but the Portainer config will look similar to the following, customized for your environment.

version: "3"

services:
   portainer:
    image: portainer/portainer-ce:latest
    container_name: portainer
    restart: always
    networks:
       - traefik
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.portainer.rule=Host(`portainer.mydomain.com`)"
      - "traefik.http.routers.portainer.tls=true"
      - "traefik.http.routers.portainer.entrypoints=websecure"
      - "traefik.http.services.portainer.loadbalancer.server.scheme=https"
      - "traefik.http.services.portainer.loadbalancer.server.port=9443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - portainer_data:/data

volumes:
  portainer_data:

ย 

That's it, once you run a docker-compose up -d your Docker Compose stack will come up and you will have your Docker containers running that are configured using Docker Compose.ย