Docker compose setup for traefik https using cloudflare DNS resolver
When using cloudflare as my dns, traefik did not work as expected when using it on docker compose. Here is how I solve it
When using cloudflare as my dns, traefik did not work as expected when using it on docker compose. Here is how I solve it.
*In this case, I turn my proxy off. Haven’t tried with proxie on, so feel free to try.
Here’s my docker-compose file for production
version: '3.8'
services:
api:
build:
context: .
env_file:
- .env
expose:
- 3000
restart: always
# volumes:
# - ./:/app
command:
yarn start
labels:
- "traefik.http.routers.api_app.rule=Host(`yourdomain.com`)"
- "traefik.http.routers.api_app.entrypoints=websecure"
- "traefik.http.routers.api_app.tls.certresolver=myresolver"
- "traefik.http.services.api_app.loadbalancer.server.port=3000"
traefik:
image: traefik:v2.5
command:
- --providers.docker
- --entrypoints.web.address=:80
- --entrypoints.websecure.address=:443
- --certificatesresolvers.myresolver.acme.dnschallenge=true
- --certificatesresolvers.myresolver.acme.dnschallenge.provider=cloudflare
- --certificatesresolvers.myresolver.acme.email=YOUR_EMAIL@mail.com
- --certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json
ports:
- "80:3000"
- "443:443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./letsencrypt:/letsencrypt
environment:
- CF_API_EMAIL=YOUR_CLOUDFLARE_EMAIL@mail.com
- CF_API_KEY=YOUR_CLODUFLARE_API_KEY
Explanation
I have two iamge image:
- First, my actual application (api) that run on PORT 3000.
- Second is traefik setup named “traefik”.
What to watch out:
- In labels for the app: make sure change rule Host(…) to your own domain
- In labels for the app: make sure server.port pointing to your used PORT
- In command of traefik: set dnschallenge.provider to cloudflare
- In command of traefik: set resolver email to your email
- In environment: change CF_API_EMAIL with your cloudflare email
- In environment: change CF_API_KEY to your cloudflare api key (could be global API KEY)
Disclaimer
I’m not a dev ops, so there might be a better solution out there, so far this is what’s working for me.