How to Install Portainer on Docker Using Docker Compose

Portainer is a powerful yet user-friendly container management tool that simplifies the deployment and monitoring of Docker environments. Whether you’re a beginner or an experienced Docker user, Portainer offers a graphical interface to manage containers, images, networks, and more.

This guide will walk you through installing Portainer on Docker using Docker Compose, ensuring you have a reliable and efficient container management system.


Requirements:


Step-by-Step Installation Guide

Step 1: Create a Docker Volume

To persist Portainer data, create a dedicated Docker volume:

docker volume create portainer_data

This volume ensures that your Portainer settings and configurations are retained even if the container is removed or updated.


Step 2: Run Portainer Using Docker

Use the following command to start the Portainer container:

docker run -d -p 8000:8000 -p 9443:9443 -p 9000:9000 --name portainer \
   --restart=always \
   -v /var/run/docker.sock:/var/run/docker.sock \
   -v portainer_data:/data \
   portainer/portainer-ce:latest

Explanation of the Flags Used:

  • -d: Runs the container in detached mode, so it runs in the background.
  • -p: Maps the container ports (8000, 9443, and 9000) to the host machine.
  • -v:
    • Mounts the Docker socket to allow Portainer to communicate with Docker.
    • Mounts the portainer_data volume to /data in the container for persistent storage.
  • --name: Assigns a custom name to the container, in this case, portainer.
  • --restart=always: Ensures the container restarts automatically if it stops unexpectedly.

Step 3: Access the Portainer Interface

Once the container is running, open your web browser and navigate to:

http://localhost:9000

You will be prompted to create an admin user and password upon first access.


Step 4: Updating Portainer

To update Portainer, follow these steps:

Stop and Remove the Old Container:

docker stop portainer
docker rm portainer

Pull the Latest Image:

docker pull portainer/portainer-ce:latest

Start the Updated Container:

docker run -d -p 8000:8000 -p 9443:9443 -p 9000:9000 --name portainer \
   --restart=always \
   -v /var/run/docker.sock:/var/run/docker.sock \
   -v portainer_data:/data \
   portainer/portainer-ce:latest

This process ensures you have the latest version of Portainer without losing your existing configurations.


Using Docker Compose to Install Portainer

If you prefer using Docker Compose, here’s how you can do it:

Create a docker-compose.yml File

Create a file named docker-compose.yml with the following content:

version: '3.8'
services:
  portainer:
    image: portainer/portainer-ce:latest
    container_name: portainer
    restart: always
    ports:
      - "8000:8000"
      - "9443:9443"
      - "9000:9000"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - portainer_data:/data
volumes:
  portainer_data:

Deploy Portainer Using Docker Compose

Run the following commands to deploy Portainer:

docker-compose up -d

This will start the Portainer container as defined in the docker-compose.yml file.


Conclusion

Portainer is an excellent tool for managing Docker environments with ease. Whether you’re deploying containers, managing networks, or monitoring resources, Portainer simplifies these tasks through an intuitive web interface.

For more information, explore the official Portainer documentation.


This comprehensive guide covers the essential steps to install Portainer, update it, and manage it effectively. By following these instructions, you can enhance your Docker management experience.