OneBite.Dev - Coding blog in a bite size

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:

What to watch out:

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.

traefik devops