Fix traefik behavior when network_mode is host

This commit is contained in:
FuNK3Y 2020-02-11 11:56:05 +01:00 committed by GitHub
parent 115d42e0f0
commit 0c90f6afa2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View file

@ -116,6 +116,20 @@ Ports detection works as follows:
by using the label `traefik.http.services.<service_name>.loadbalancer.server.port`
(Read more on this label in the dedicated section in [routing](../routing/providers/docker.md#port)).
### Host networking
When exposing containers that are configured with [host networking](https://docs.docker.com/network/host/),
the IP address of the host is resolved as follows:
<!-- TODO: verify and document the swarm mode case with container.Node.IPAddress coming from the API -->
- try a lookup of `host.docker.internal`
- otherwise fall back to `127.0.0.1`
On Linux, (and until [github.com/moby/moby/pull/40007](https://github.com/moby/moby/pull/40007) is included in a release),
for `host.docker.internal` to be defined, it should be provided as an `extra_host` to the Traefik container,
using the `--add-host` flag. For example, to set it to the IP address of the bridge interface (docker0 by default):
`--add-host=host.docker.internal:172.17.0.1`
### Docker API Access
Traefik requires access to the docker socket to get its dynamic configuration.

View file

@ -259,6 +259,9 @@ func (p Provider) getIPAddress(ctx context.Context, container dockerData) string
if container.Node != nil && container.Node.IPAddress != "" {
return container.Node.IPAddress
}
if host, err := net.LookupHost("host.docker.internal"); err == nil {
return host[0]
}
return "127.0.0.1"
}