GET Request on Docker Traefik API - Block by CORS rules

Viewed 5513

I've got trouble finding how to reach my Traefik API using GET request from an other docker container.

Context

I have 2 docker container, one running a traefik instance :

docker run -d -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 -p 80:80 -l traefik.frontend.rule=Host:my.server -l traefik.port=80 --name traefik traefik --docker --api

The other container run a quite simple React web application. I'm trying to reach the Traefik API using XMLHttpRequest.

My request is blocked by the Traefik CORS Policy. Any idea on how configure Traefik to enable Access-Control-Allow-Origin ?

1 Answers

The only way I've done this is by wiring the Access-Control-Allow-Origin header as a label in the Træfik container.

traefik.frontend.headers.customResponseHeaders=Access-Control-Allow-Origin:*

And if you need to add more headers like Access-Control-Allow-Method, you must concatenate them with double bars (||) in the same rule.

traefik.frontend.headers.customResponseHeaders=Hdr1:val1,val2||Hdr2:val3

So your docker run line would be as follows.

docker run -d -v /var/run/docker.sock:/var/run/docker.sock \
-p 8080:8080 -p 80:80 \
-l traefik.frontend.rule=Host:my.server \
-l traefik.port=80 \
-l "traefik.frontend.headers.customResponseHeaders=Access-Control-Allow-Origin:*" \
--name traefik traefik --docker --api
Related