How to config an API Gateway in docker compose?

Hi everyone, I’m a new user.
In my docker-compose.yaml file I have two microservices:

version: '3'

x-common-settings: &common-settings
  restart: on-failure

services:

  user-service:
    <<: *common-settings
    container_name: user-service
    build: ./user-service
    ports:
      - 8081:8080
  
  purchase-service:
    <<: *common-settings
    container_name: purchase-service
    build: ./purchase-service
    ports:
      - 8082:8080

At this moment, if I want to test my APIs, I have to go to localhost:8081/hello for microservice1 and to localhost:8082/hello for microservice2.
Instead, i would test my APIs going to localhost/users/hello and localhost/purchases/hello, using the same port (8080). How could I do it? Thanks in advance.
I read on the web that the solution is Traefik, but trying to emulate the example in the documentation I was unable to get it to work properly.

That was the right way. Alternatively you can use nginx proxy which was easy to me, but some people say they could not configure nginx proxy so they switched to traefik and it worked.

If you share how you tried to configure traefik and what your problem was, I am sure we can point you to the right direction or even tell you what you did wrong.

1 Like

Thank you. At the end I tried to use nginx proxy and It worked