1

I have a compose.yaml serving WordPress with a Traefik reverse proxy. The https port 443 loopback for the WordPress container fails by default because the WordPress container only listens on http port 80. I need to have the loopback go through the reverse proxy. I can do this by adding the site's domain to the extra_hosts, but I have to hardcode the traefik container IP address, which happens to be 172.18.0.2. What's the proper way to add a record to the WordPress container's /etc/hosts file with the Traefik container's IP address?

services:
  traefik:
    image: traefik
    ports:
      - 80:80
      - 443:443
    ...

  wordpress:
    image: wordpress
    extra_hosts:
      # how to avoid hardcoding traefik container IP here?
      - mysite.com:172.18.0.2
    ...
2
  • Why do you need to do this? mysite.com is going to resolve to the swarms external ip anyway, which is where traefik is listening anyway. Commented Aug 21, 2023 at 10:04
  • I don't know why it doesn't work by default, but WordPress's health check fails the loopback check with a port 443 connection refused. This wouldn't be coming from the external IP port 443 where https traffic routes through Traefik as expected. The WordPress docker container doesn't listen on port 443 though, only port 80. If I add the external IP address for mysite.com as an extra_hosts, then the loopback check fails with a connection timeout. Either using the Traefik container's IP as an extra_hosts or the alias solution resolves the issue with the loopback check finally succeeding. Commented Aug 23, 2023 at 7:56

1 Answer 1

1

Solution from here:

What’s the proper way to add a record to the WordPress container’s /etc/hosts file with the Traefik container’s IP address?

The proper way is not adding anything to the hosts file but setting a network alias:

https://docs.docker.com/engine/reference/commandline/network_connect/ 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 external network and you need to set the alias on that network and not on the default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.