Skip to content

Commit 69dcccf

Browse files
authored
Merge pull request #4399 from dvdksn/docs/host-gateway
docs: document special host-gateway value for add-host
2 parents d1c04fb + 299925f commit 69dcccf

3 files changed

Lines changed: 47 additions & 14 deletions

File tree

docs/reference/commandline/build.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,17 @@ You can add other hosts into a container's `/etc/hosts` file by using one or
458458
more `--add-host` flags. This example adds a static address for a host named
459459
`docker`:
460460

461-
$ docker build --add-host=docker:10.180.0.1 .
461+
```console
462+
$ docker build --add-host docker:10.180.0.1 .
463+
```
464+
465+
If you need your build to connect to services running on the host, you can use
466+
the special `host-gateway` value for `--add-host`. In the following example,
467+
build containers resolve `host.docker.internal` to the host's gateway IP.
468+
469+
```console
470+
$ docker build --add-host host.docker.internal:host-gateway .
471+
```
462472

463473
### <a name="target"></a> Specifying target build stage (--target)
464474

docs/reference/commandline/dockerd.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,25 @@ the host.
833833
For details about how to use this feature, as well as limitations, see
834834
[Isolate containers with a user namespace](https://docs.docker.com/engine/security/userns-remap/).
835835

836+
### Configure host gateway IP
837+
838+
The Docker daemon supports a special `host-gateway` value for the `--add-host`
839+
flag for the `docker run` and `docker build` commands. This value resolves to
840+
the host's gateway IP and lets containers connect to services running on the
841+
host.
842+
843+
By default, `host-gateway` resolves to the IP address of the default bridge.
844+
You can configure this to resolve to a different IP using the `--host-gateway-ip`
845+
flag for the dockerd command line interface, or the `host-gateway-ip` key in
846+
the daemon configuration file.
847+
848+
```console
849+
$ dockerd --host-gateway-ip 192.0.2.0
850+
$ docker run -it --add-host host.docker.internal:host-gateway \
851+
busybox ping host.docker.internal
852+
PING host.docker.internal (192.0.2.0): 56 data bytes
853+
```
854+
836855
### Miscellaneous options
837856

838857
IP masquerading uses address translation to allow containers without a public

docs/reference/commandline/run.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -760,24 +760,28 @@ PING docker (93.184.216.34): 56 data bytes
760760
round-trip min/avg/max = 92.209/92.495/93.052 ms
761761
```
762762

763-
Sometimes you need to connect to the Docker host from within your
764-
container. To enable this, pass the Docker host's IP address to
765-
the container using the `--add-host` flag. To find the host's address,
766-
use the `ip addr show` command.
763+
The `--add-host` flag supports a special `host-gateway` value that resolves to
764+
the internal IP address of the host. This is useful when you want containers to
765+
connect to services running on the host machine.
767766

768-
The flags you pass to `ip addr show` depend on whether you are
769-
using IPv4 or IPv6 networking in your containers. Use the following
770-
flags for IPv4 address retrieval for a network device named `eth0`:
767+
It's conventional to use `host.docker.internal` as the hostname referring to
768+
`host-gateway`. Docker Desktop automatically resolves this hostname, see
769+
[Explore networking features](https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host).
770+
771+
The following example shows how the special `host-gateway` value works. The
772+
example runs an HTTP server that serves a file from host to container over the
773+
`host.docker.internal` hostname, which resolves to the host's internal IP.
771774

772775
```console
773-
$ HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print $2}' | cut -d / -f 1 | sed -n 1p`
774-
$ docker run --add-host=docker:${HOSTIP} --rm -it debian
776+
$ echo "hello from host!" > ./hello
777+
$ python3 -m http.server 8000
778+
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
779+
$ docker run \
780+
--add-host host.docker.internal:host-gateway \
781+
curlimages/curl -s host.docker.internal:8000/hello
782+
hello from host!
775783
```
776784

777-
For IPv6 use the `-6` flag instead of the `-4` flag. For other network
778-
devices, replace `eth0` with the correct device name (for example `docker0`
779-
for the bridge device).
780-
781785
### <a name="ulimit"></a> Set ulimits in container (--ulimit)
782786

783787
Since setting `ulimit` settings in a container requires extra privileges not

0 commit comments

Comments
 (0)