Skip to content

Conversation

thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Feb 13, 2020

carry of #2293
closes #2293

Relates to - moby/moby#40007 "Support host.docker.internal in dockerd on Linux"
The above PR added support in moby, that detects if
a special string "host-gateway" is added to the IP
section of --add-host, and if true, replaces it with
a special IP value (value of --host-gateway-ip Daemon flag
which defaults to the IP of the default bridge).

This PR is needed to skip the validation for the above
feature

opts/hosts.go Outdated
@@ -6,6 +6,8 @@ import (
"net/url"
"strconv"
"strings"

"github.com/docker/docker/daemon/network"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should move the constant to somewhere in the client/api package; we're now importing the daemon package, which is not really ideal

Relates to - moby/moby 40007
The above PR added support in moby, that detects if
a special string "host-gateway" is added to the IP
section of --add-host, and if true, replaces it with
a special IP value (value of --host-gateway-ip Daemon flag
which defaults to the IP of the default bridge).

This PR is needed to skip the validation for the above
feature

Signed-off-by: Arko Dasgupta <arko.dasgupta@docker.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah force-pushed the carry_2293_add_host_gateway_dns_entry branch from 4688f28 to 67ebcd6 Compare February 27, 2020 15:22
Comment on lines +27 to +32
// hostGatewayName defines a special string which users can append to --add-host
// to add an extra entry in /etc/hosts that maps host.docker.internal to the host IP
// TODO Consider moving the HostGatewayName constant defined in docker at
// github.com/docker/docker/daemon/network/constants.go outside of the "daemon"
// package, so that the CLI can consume it.
hostGatewayName = "host-gateway"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went back to defining a const locally, but adding a TODO to consider improving this.

Copy link
Contributor

@silvin-lubecki silvin-lubecki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@silvin-lubecki silvin-lubecki merged commit 2298e6a into docker:master Feb 27, 2020
@GordonTheTurtle GordonTheTurtle added this to the 20.03.0 milestone Feb 27, 2020
@thaJeztah thaJeztah deleted the carry_2293_add_host_gateway_dns_entry branch February 27, 2020 17:00
@thaJeztah
Copy link
Member Author

Working on updated documentation for this, but splitting it in "enhancements to be backported" and "for next release"

@arkodg
Copy link
Contributor

arkodg commented Feb 27, 2020

@thaJeztah and @silvin-lubecki thanks for taking care of this !

@thaJeztah
Copy link
Member Author

@arkodg you're welcome! And sorry for the back-and-forth on the const. I didn't realise it was in the "daemon" package at the time.

@jcoutch
Copy link

jcoutch commented Apr 13, 2020

@thaJeztah - Will support for host-gateway be added to docker/compose? While it works using the docker cli directly, it looks like compose has it's own IP address validation.

@thaJeztah
Copy link
Member Author

@jcoutch it's supported by the API / daemon, so would be able to be supported by compose if the validation is removed; can you open a ticket in the docker compose repository? (or perhaps even better; a pull request if you're familiar with Python

@thaJeztah
Copy link
Member Author

@jcoutch I just tested docker compose against a daemon that supports this feature;

docker-compose version

docker-compose version 1.26.0-rc3, build 46118bc5
docker-py version: 4.2.0
CPython version: 3.7.6
OpenSSL version: OpenSSL 1.1.1d  10 Sep 2019

Using this compose file:

version: '3.6'
services:
  nginx:
    image: nginx:alpine
    extra_hosts:
      - "foo:127.0.0.1"
      - "bar:127.0.0.2"
      - "host.docker.internal:host-gateway"

Trying to run it on a docker 19.03 daemon produces an error (as expected);

docker-compose up -d
Creating network "extra-hosts_default" with the default driver
Creating extra-hosts_nginx_1 ... error

ERROR: for extra-hosts_nginx_1  Cannot create container for service nginx: invalid IP address in add-host: "host-gateway"

ERROR: for nginx  Cannot create container for service nginx: invalid IP address in add-host: "host-gateway"
ERROR: Encountered errors while bringing up the project.

On a docker daemon built from master (so with support for the host-gateway feature);

docker-compose up -d

Creating network "extra-hosts_default" with the default driver
Creating extra-hosts_nginx_1 ... done

Verifying the container that's created:

docker inspect --format='{{json .HostConfig.ExtraHosts}}' extra-hosts_nginx_1

Which produces:

[
  "foo:127.0.0.1",
  "bar:127.0.0.2",
  "host.docker.internal:host-gateway"
]

And verifying that the magic host-gateway value is replaced correctly inside the container;

docker exec extra-hosts_nginx_1 cat /etc/hosts

127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
127.0.0.1	foo
127.0.0.2	bar
172.18.0.1	host.docker.internal
172.20.0.2	2eb3a4602c9a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants