From 0c90f6afa24ef390fec43ca654f806915e821daa Mon Sep 17 00:00:00 2001 From: FuNK3Y Date: Tue, 11 Feb 2020 11:56:05 +0100 Subject: [PATCH] Fix traefik behavior when network_mode is host --- docs/content/providers/docker.md | 14 ++++++++++++++ pkg/provider/docker/config.go | 3 +++ 2 files changed, 17 insertions(+) diff --git a/docs/content/providers/docker.md b/docs/content/providers/docker.md index d76635ea2..db0b33b1f 100644 --- a/docs/content/providers/docker.md +++ b/docs/content/providers/docker.md @@ -116,6 +116,20 @@ Ports detection works as follows: by using the label `traefik.http.services..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: + + +- 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. diff --git a/pkg/provider/docker/config.go b/pkg/provider/docker/config.go index 5ffc00798..3108e0c3d 100644 --- a/pkg/provider/docker/config.go +++ b/pkg/provider/docker/config.go @@ -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" }