Search code examples
node.jsdockerporttraefik

How can I access my API using Traefik + Docker + Nodejs without the port number in the URL?


It might be a duplicate of : Accessing container on port 3000 thru traefik but this solution is with v1 of traefik.

Hello guys , currently i was developing two things:

  • react app on port 3001
  • node app on port 3000

Later they will work on the same domain, and i want to work that way while developing.

I decided to initiate myself to traefik for this need ..

version: '3.7'

services:
  traefik:
    image: traefik:v2.2
    command: 
      - --api.insecure=true 
      # Docker
      - --providers.docker 
      - --providers.docker.exposedByDefault=false 
      - --providers.docker.network=internal
      - --entrypoints.web.address=:80v02.api.letsencrypt.org/directory
      # Config
      - --accesslog=true
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
  whoami:
    image: emilevauge/whoami
    labels:
      - "traefik.http.routers.whoami.rule=Host(`whoami.docker.localhost.test`)"
      - "traefik.enable=true"
  api:
    container_name: api
    build:
      context: .
      target: development
    labels:
      #- "traefik.http.services.api-service.loadbalancer.server.port=3000"
      - "traefik.http.routers.api-router.rule=Host(`api.brainstoo.localhost.test`)"
      - "traefik.enable=true"

Right now i can access my api with the url : api.brainstoo.localhost.test:3000 But i would like to access it directly with api.brainstoo.localhost.test without the port. Wether i comment or uncomment the line ** #- "traefik.http.services.apiservice.loadbalancer.server.port=3000"** changes nothing.

I don't know what i m doing wrong. Maybe i did not understand well if traefik would resolve this kind of matters.

NB : i already configured my /etc/hosts file

Thanks in advance.


Solution

  • My docker configuration for my app was too long, but. i should have posted it all because it was a network issue !

    Now that everything is on the same network it works !