I've created a project with React in the front end and Node+express in the back end. I dockerized my frontend and backend, and would like to add traefik so that when I do (in local for now):
localhost:3000/api/products (3000 is the port used in the frontend/client), it redirects to localhost:5000/api/products (5000 is the port used in the backend/server) and then sends back the response to my frontend.
and here is the traefik.toml file that I have created in the root of my project:
# http routing section
# Define a connection between requests and services
rule = "Host(`localhost`) && PathPrefix(`/api/`)"
service = "lg-server"
# Define how to reach an existing service on our infrastructure
url = "http://localhost:5000/"
endpoint = "unix:///var/run/docker.sock"
domain = "localhost"
watch = true
exposedbydefault = false
In the frontend/client, I have added in the package.json:
When I hit docker-compose up --build, I can open my localhost:3000 correctly. localhost:5000/api/products also returns the right response.
However, localhost:3000/api/products doesn't. It returns my public/index.html!
It worked correctly before I dockerised my application and started using traefik, if I just have in the package.json:
Your traefik is only bound to 80 (and 8080 for api insecure) so localhost:3000 will be going to your container direct per its 3000:3000 port option.
Additionally the router rules are incorrect as they do not have a router identifier:
# should have an identifier and the rule format is incorrect:
# written this way the router will only be accessible from the docker host
service(aside from not having a router identifier) is not required as the container is the service
If the container does not EXPOSE a port in its docker file you may need:
Thanks so much for getting back to me with these answers, that's very helpful!!
I tried modifying my files accordingly but am still struggling to make it work properly. I realise there are some areas I didn't understand quite well enough...
When you say that, does it mean that in local I'm supposed to hit localhost:80 instead of localhost:3000 now that I'll use Traefik? Or should I define 3000 as an entrypoint? Or maybe use the host that I can see in the Traefik dashboard under "Used by Routers" (client-myapp)?
Where can I find this reachable address? Is it the one I can see in the Traefik dashboard in the server url of my server? http://192.168.208.2:5000?
Also, what am I supposed to see in the dashboard to make sure that my router is working correctly?
I'll be able to see the server, client and reverse-proxy in the services and router, but shouldn't I see that client -> reverse-proxy -> server ? or something like this?
I tried defining this entrypoint in the traefik.toml but wonder now if I need to add the labels in the client service in docker as well? I find it a bit confusing to have traefik labels in docker + traefik config in the traefik.toml
Are your trying to proxy your containers or are there actually other services running locally?
You don't need to, it depends on what your are doing. If you are routing to a service that is not a container in your docker (external service) the you do need to use a File Provider it may help cement some concepts.
Personally I wouldn't use Traefik to address other containers but rather user docker networking.
If you are determined to do it you need to make sure that the Host you are using in the router rule is resolved by the container to the Traefik container. It would be easier NOT to use a Host rule in this case.