Skip to main content

Installing Portainer


Chapter objectives

  • Install Portainer on Docker standalone
  • Deploy Portainer on Docker Swarm
  • Configure the Portainer agent
  • Secure the installation

1 - Installation on Docker Standalone

Quick method

# Créer le volume de données
docker volume create portainer_data

# Déployer Portainer CE
docker run -d \
-p 8000:8000 \
-p 9443:9443 \
--name portainer \
--restart=always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v portainer_data:/data \
portainer/portainer-ce:latest

Explanation of the parameters

ParameterDescription
-p 9443:9443HTTPS port of the web interface
-p 8000:8000Port for the Edge agent
--restart=alwaysAutomatic restart
-v /var/run/docker.sockAccess to the Docker daemon
-v portainer_data:/dataData persistence

With Docker Compose

# docker-compose.yml
version: "3.9"

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

volumes:
portainer_data:
docker compose up -d

First access

  1. Open https://localhost:9443
  2. Accept the self-signed certificate
  3. Create the administrator account (strong password)
  4. Select "Get Started" for the local environment

2 - Installation on Docker Swarm

Deployment as a service

# Sur un manager Swarm
docker service create \
--name portainer \
--publish 9443:9443 \
--publish 8000:8000 \
--replicas=1 \
--constraint 'node.role==manager' \
--mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
--mount type=volume,src=portainer_data,dst=/data \
portainer/portainer-ce:latest
# portainer-stack.yml
version: "3.9"

services:
agent:
image: portainer/agent:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /var/lib/docker/volumes:/var/lib/docker/volumes
networks:
- agent_network
deploy:
mode: global
placement:
constraints:
- node.platform.os == linux

portainer:
image: portainer/portainer-ce:latest
command: -H tcp://tasks.agent:9001 --tlsskipverify
ports:
- "9443:9443"
- "8000:8000"
volumes:
- portainer_data:/data
networks:
- agent_network
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.role == manager

networks:
agent_network:
driver: overlay
attachable: true

volumes:
portainer_data:
docker stack deploy -c portainer-stack.yml portainer

Verification

# Services déployés
docker service ls | grep portainer

# Logs
docker service logs portainer_portainer

3 - Portainer agent

Why the agent?

The Portainer agent allows you to:

  • Manage remote environments
  • Access advanced features (browse volumes)
  • Avoid exposing the Docker socket
┌─────────────────────────────────────────────────────────────┐
│ Portainer Server │
│ (Centralisé) │
└──────────────────────────┬──────────────────────────────────┘
│ Port 9001
┌────────────────┼────────────────┐
│ │ │
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Agent │ │ Agent │ │ Agent │
│ Host 1 │ │ Host 2 │ │ Host 3 │
└─────────┘ └─────────┘ └─────────┘

Deploy the agent (standalone)

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 \
portainer/agent:latest

Deploy the agent (Swarm)

docker service create \
--name portainer_agent \
--network portainer_agent_network \
--mode global \
--constraint 'node.platform.os==linux' \
--mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
--mount type=bind,src=/var/lib/docker/volumes,dst=/var/lib/docker/volumes \
portainer/agent:latest

Connect the agent to Portainer

  1. In Portainer, go to Environments
  2. Click on Add environment
  3. Select Agent
  4. Enter: <IP_AGENT>:9001
  5. Name the environment
  6. Click on Connect

4 - Edge Agent

Use case

The Edge Agent is useful for:

  • Environments behind NAT/firewall
  • Outbound connection only
  • Remote sites without a public IP

Deployment

  1. In Portainer, create an Edge environment
  2. Copy the generated command
  3. Run it on the remote host:
docker run -d \
--name portainer_edge_agent \
--restart always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /var/lib/docker/volumes:/var/lib/docker/volumes \
-v /:/host \
-v portainer_agent_data:/data \
-e EDGE=1 \
-e EDGE_ID=<EDGE_ID> \
-e EDGE_KEY=<EDGE_KEY> \
-e EDGE_INSECURE_POLL=1 \
portainer/agent:latest

5 - Advanced configuration

Environment variables

docker run -d \
-p 9443:9443 \
--name portainer \
-e ADMIN_PASSWORD='$2y$...' \ # Hash bcrypt du mot de passe
-e LOGO_URL='https://example.com/logo.png' \
-v /var/run/docker.sock:/var/run/docker.sock \
-v portainer_data:/data \
portainer/portainer-ce:latest
VariableDescription
ADMIN_PASSWORDbcrypt hash of the admin password
LOGO_URLURL of the custom logo
TEMPLATES_URLURL of the custom templates

Generate the password hash

# Avec htpasswd
htpasswd -nbB admin 'votre-mot-de-passe' | cut -d ":" -f 2

# Ou avec Docker
docker run --rm httpd:alpine htpasswd -nbB admin 'votre-mot-de-passe' | cut -d ":" -f 2

HTTPS with a custom certificate

docker run -d \
-p 9443:9443 \
--name portainer \
-v /path/to/certs:/certs \
-v /var/run/docker.sock:/var/run/docker.sock \
-v portainer_data:/data \
portainer/portainer-ce:latest \
--ssl \
--sslcert /certs/cert.pem \
--sslkey /certs/key.pem

6 - Installation with Traefik

Docker Compose with Traefik

version: "3.9"

services:
traefik:
image: traefik:v2.10
command:
- "--api.dashboard=true"
- "--providers.docker=true"
- "--entrypoints.web.address=:80"
- "--entrypoints.websecure.address=:443"
- "[email protected]"
- "--certificatesresolvers.le.acme.storage=/letsencrypt/acme.json"
- "--certificatesresolvers.le.acme.httpchallenge.entrypoint=web"
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- letsencrypt:/letsencrypt

portainer:
image: portainer/portainer-ce:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- portainer_data:/data
labels:
- "traefik.enable=true"
- "traefik.http.routers.portainer.rule=Host(`portainer.example.com`)"
- "traefik.http.routers.portainer.entrypoints=websecure"
- "traefik.http.routers.portainer.tls.certresolver=le"
- "traefik.http.services.portainer.loadbalancer.server.port=9000"

volumes:
portainer_data:
letsencrypt:

7 - Updating Portainer

Standalone

# Arrêter et supprimer le conteneur
docker stop portainer
docker rm portainer

# Télécharger la nouvelle image
docker pull portainer/portainer-ce:latest

# Redéployer (les données sont dans le volume)
docker run -d \
-p 8000:8000 \
-p 9443:9443 \
--name portainer \
--restart=always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v portainer_data:/data \
portainer/portainer-ce:latest

Swarm

# Mettre à jour le service
docker service update \
--image portainer/portainer-ce:latest \
portainer_portainer

# Mettre à jour l'agent
docker service update \
--image portainer/agent:latest \
portainer_agent

8 - Troubleshooting

Portainer does not start

# Vérifier les logs
docker logs portainer

# Vérifier le socket Docker
ls -la /var/run/docker.sock

# Permissions du socket
sudo chmod 666 /var/run/docker.sock

Unable to connect

# Vérifier que le conteneur tourne
docker ps | grep portainer

# Vérifier les ports
docker port portainer

# Tester la connectivité
curl -k https://localhost:9443

Reset the admin password

# Arrêter Portainer
docker stop portainer

# Réinitialiser
docker run --rm -v portainer_data:/data portainer/helper-reset-password

# Redémarrer
docker start portainer

Summary

ModeKey command
Standalonedocker run ... portainer/portainer-ce
Swarmdocker stack deploy with agent
Agentdocker run ... portainer/agent
EdgeAgent with outbound connection
Key points
  • Always create a volume for persistence
  • Use the agent for remote environments
  • HTTPS enabled by default on port 9443
  • Data survives updates

Practical exercises

  1. Install Portainer on your local machine
  2. Create a secure administrator account
  3. Add a remote environment with the agent
  4. Configure HTTPS with a custom certificate

← Introduction | User interface →