What is Portainer?
Portainer is a powerful, open-source container management platform that allows you to easily create, manage, and maintain Docker environments. Below is the portainer architecture.
What is Traefik?
Traefik is an easy, dynamic, automatic and fast open source reverse proxy and load balancer for HTTP and TCP based applications.
Traefik and container working principle is below.
Docker must be installed before starting the Portainer installation.
- sudo apt-get remove docker docker-engine docker.io containerd runc
- sudo apt-get update
- sudo apt-get install \
- sudo mkdir -m 0755 -p /etc/apt/keyrings
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- echo \
Update apt packages and install the latest versions for Docker engine,containerd and docker compose;
- sudo apt-get update
- sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
- sudo docker run hello-world
Traefik and Portainer Installation
Filesystem with a structure similar to the one below create;
- Home directory where all Docker related stuff will be stored:
/home/ubuntu/docker
- Core with docker-compose for Traefik and Portainer directory:
- Traefik configuration files
- This file is for storing all certificates used
- Configuration for Traefik file
- Portainer configuration files
Run the following commands in the terminal one by one to create the files;
- mkdir -p /home/ubuntu/docker/core/traefik-data
- mkdir -p /home/ubuntu/docker/core/portainer-data
- touch /home/ubuntu/docker/core/traefik-data/acme.json
- chmod 600 Open /home/ubuntu/docker/core/traefik-data/acme.json
- touch /home/ubuntu/docker/core/traefik-data/traefik.yml
traefik.yml and edit it as follows. Replace email@example.com with your email address https:
address: ":443"
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
certificatesResolvers:
http:
acme:
email: email@example.com
storage: acme.json
httpChallenge:
entryPoint: http
Create a secure password for Traefik access. To do this, first install the htpasswd package and run the following commands one by one;
- sudo apt install apache2-utils
- echo $(htpasswd -nb
) | sed -e s/\\$/\\$\\$/g
Create a docker Proxy network;
- docker network create traefik-proxy
Edit the docker-compose.yml file as follows;
version: '3'
services:
traefik:
image: traefik:v2.2
container_name: traefik
restart: unless-stopped
security_opt:
- no-new-privileges:true
networks:
- traefik-proxy
ports:
- 80:80
- 443:443
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./traefik-data/traefik.yml:/traefik.yml:ro
- ./traefik-data/acme.json:/acme.json
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.entrypoints=http"
- "traefik.http.routers.traefik.rule=Host(`traefik.example.com`)"
- "traefik.http.middlewares.traefik-auth.basicauth.users=username:password"
- "traefik.http.middlewares.traefik-https-redirect.redirectscheme.scheme=https"
- "traefik.http.routers.traefik.middlewares=traefik-https-redirect"
- "traefik.http.routers.traefik-secure.entrypoints=https"
- "traefik.http.routers.traefik-secure.rule=Host(`traefik.example.com`)"
- "traefik.http.routers.traefik-secure.middlewares=traefik-auth"
- "traefik.http.routers.traefik-secure.tls=true"
- "traefik.http.routers.traefik-secure.tls.certresolver=http"
- "traefik.http.routers.traefik-secure.service=api@internal"
portainer:
image: portainer/portainer:latest
container_name: portainer
restart: unless-stopped
security_opt:
- no-new-privileges:true
networks:
- traefik-proxy
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./portainer-data:/data
labels:
- "traefik.enable=true"
- "traefik.http.routers.portainer.entrypoints=http"
- "traefik.http.routers.portainer.rule=Host(`portainer.example.com`)"
- "traefik.http.middlewares.portainer-https-redirect.redirectscheme.scheme=https"
- "traefik.http.routers.portainer.middlewares=portainer-https-redirect"
- "traefik.http.routers.portainer-secure.entrypoints=https"
- "traefik.http.routers.portainer-secure.rule=Host(`portainer.example.com`)"
- "traefik.http.routers.portainer-secure.tls=true"
- "traefik.http.routers.portainer-secure.tls.certresolver=http"
- "traefik.http.routers.portainer-secure.service=portainer"
- "traefik.http.services.portainer.loadbalancer.server.port=9000"
- "traefik.docker.network=traefik-proxy"
networks:
traefik-proxy:
external: true
Finally, run the following command to stand up the containers;
- docker-compose up -d
You can access the portainer interface by going to https://portainer.example.com from your browser. After creating a username and password on the screen that appears, the system will be ready for use.