<p>Solution from <a href="https://forums.docker.com/t/specify-container-ip-as-extra-hosts/137037/2" rel="nofollow noreferrer">here</a>:</p>
<blockquote>
<p>What’s the proper way to add a record to the WordPress container’s <code>/etc/hosts</code> file with the Traefik container’s IP address?</p>
</blockquote>
<p>The proper way is not adding anything to the hosts file but setting a network alias:</p>
<p><a href="https://docs.docker.com/engine/reference/commandline/network_connect/" rel="nofollow noreferrer">https://docs.docker.com/engine/reference/commandline/network_connect/</a>
<a href="https://docs.docker.com/compose/compose-file/06-networks/" rel="nofollow noreferrer">https://docs.docker.com/compose/compose-file/06-networks/</a></p>
<pre><code>services:
  traefik:
    networks:
      default:
        aliases:
          - mysite.com
</code></pre>
<p>It registers the domain as an alias for the container in Docker’s built-in DNS server.</p>
<p>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.</p>

CC BY-SA 4.0
2023-08-01T00:12:25.453Z
2023-08-01T00:12:25.453Z
Jeff Lockhart
<p>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 <code>extra_hosts</code>, 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 <code>/etc/hosts</code> file with the Traefik container's IP address?</p>
<pre><code>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
    ...
</code></pre>

CC BY-SA 4.0
2023-08-21T10:04:22.190Z
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.
Chris Becke
CC BY-SA 4.0
2023-08-23T07:56:15.150Z
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.
Jeff Lockhart
<docker><docker-compose><traefik><wordpress>
Specify container IP as extra_hosts in Docker Compose
Jeff Lockhart
# Specify container IP as extra_hosts in Docker Compose

(Asked by: username_0 on 2023-07-29)

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
    ...

**Tags:** docker, docker-compose, traefik, wordpress

> **Comments:**
> **username_1**: 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.  
> **username_0**: 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.  

---

## 1 answers

### Answer by username_0 on 2023-08-01. Score: 1 | Accepted answer

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.


---

