@@ -760,24 +760,28 @@ PING docker (93.184.216.34): 56 data bytes
760760round-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
783787Since setting ` ulimit ` settings in a container requires extra privileges not
0 commit comments