The proper way is not adding anything to the hosts file but setting a network alias:
https://docs.docker.com/compose/compose-file/06-networks/
services:
traefik:
networks:
default:
aliases:
- mysite.com
It registers the domain as an alias for the container in Docker’s built-in DNS server.
The reverse proxy is usually in a different compose project. If that’s the case, you have an extrnal network and you need to set the alias on that network and not on the default.