If you want to use Traefik as a reverse proxy to route traffic to Portainer using a domain such as mydomain.dev.com, you can add labels to the Portainer service within your docker-compose.yml file. These labels will configure Traefik to route the traffic appropriately.
Below is an example configuration for setting up Portainer with Traefik and using the domain mydomain.dev.com:
docker-compose.yml
version: '3.3'
services:
portainer:
image: portainer/portainer-ce
ports:
- "9000:9000"
volumes:
- portainer_data:/data
- /var/run/docker.sock:/var/run/docker.sock
labels:
- "traefik.enable=true"
- "traefik.http.routers.portainer.rule=Host(`mydomain.dev.com`)"
- "traefik.http.routers.portainer.entrypoints=web"
# If you're using HTTPS:
# - "traefik.http.routers.portainer.tls=true"
deploy:
restart_policy:
condition: on-failure
volumes:
portainer_data:
Traefik Configuration
Make sure Traefik is set up correctly to manage HTTP traffic, typically done in its own docker-compose.yml (or by however you're deploying Traefik), and ensure it has a network on which both Treafik and Portainer containers can communicate. The key setup will typically include the following in Traefik's configuration:
-
Network: Ensure that both Traefik and the Portainer service are on the same Docker network so they can communicate directly. For example, if using a network named proxy, make sure both are connected to it.
-
EntryPoints: Make sure Traefik is listening on the entrypoints that your labels reference. It's common to have a web entrypoint for HTTP traffic and websecure for HTTPS.
Here's a minimal example of Traefik's setup (usually Traefik v2) with Docker as a provider:
version: '3.3'
services:
traefik:
image: traefik:v2.10
command:
- "--api.insecure=true" # remove or secure in production environments
- "--providers.docker=true"
- "--entrypoints.web.address=:80"
# Uncomment for HTTPS:
# - "--entrypoints.websecure.address=:443"
# - "--certificatesresolvers.myresolver.acme.tlschallenge=true"
# - "--certificatesresolvers.myresolver.acme.email=your-email@example.com"
# - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
ports:
- "80:80"
- "8080:8080"
# - "443:443" # Uncomment for HTTPS
volumes:
- /var/run/docker.sock:/var/run/docker.sock
# Uncomment for HTTPS
# - ./letsencrypt:/letsencrypt
networks:
- proxy
networks:
proxy:
external: true
Networking
Remember to create the proxy network if you haven't already:
docker network create proxy
Attach both the Traefik and your application services (like Portainer) to the proxy network in their respective configurations.
Domain Resolution
Make sure that mydomain.dev.com resolves to the IP address where your Docker engine is running. This can be done via a local /etc/hosts entry, DNS record, or by using a dynamic DNS service if you're behind a NAT.
Summary
This setup ensures that Traefik acts as a reverse proxy for Portainer, redirecting any requests to mydomain.dev.com to the appropriate service. If you are using HTTPS with Traefik, ensure that certificates are correctly set up with a provider like Let's Encrypt. Traefik's documentation provides further details on how to set up SSL/TLS with automatic certificate provisioning.