@@ -759,24 +759,28 @@ PING docker (93.184.216.34): 56 data bytes
759759round-trip min/avg/max = 92.209/92.495/93.052 ms
760760```
761761
762- Sometimes you need to connect to the Docker host from within your
763- container. To enable this, pass the Docker host's IP address to
764- the container using the ` --add-host ` flag. To find the host's address,
765- use the ` ip addr show ` command.
762+ The ` --add-host ` flag supports a special ` host-gateway ` value that resolves to
763+ the internal IP address of the host. This is useful when you want containers to
764+ connect to services running on the host machine.
766765
767- The flags you pass to ` ip addr show ` depend on whether you are
768- using IPv4 or IPv6 networking in your containers. Use the following
769- flags for IPv4 address retrieval for a network device named ` eth0 ` :
766+ It's conventional to use ` host.docker.internal ` as the hostname referring to
767+ ` host-gateway ` . Docker Desktop automatically resolves this hostname, see
768+ [ Explore networking features] ( https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host ) .
769+
770+ The following example shows how the special ` host-gateway ` value works. The
771+ example runs an HTTP server that serves a file from host to container over the
772+ ` host.docker.internal ` hostname, which resolves to the host's internal IP.
770773
771774``` console
772- $ HOSTIP=` ip -4 addr show scope global dev eth0 | grep inet | awk ' {print $2}' | cut -d / -f 1 | sed -n 1p`
773- $ docker run --add-host=docker:${HOSTIP} --rm -it debian
775+ $ echo " hello from host!" > ./hello
776+ $ python3 -m http.server 8000
777+ Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
778+ $ docker run \
779+ --add-host host.docker.internal:host-gateway \
780+ curlimages/curl -s host.docker.internal:8000/hello
781+ hello from host!
774782```
775783
776- For IPv6 use the ` -6 ` flag instead of the ` -4 ` flag. For other network
777- devices, replace ` eth0 ` with the correct device name (for example ` docker0 `
778- for the bridge device).
779-
780784### <a name =" ulimit " ></a > Set ulimits in container (--ulimit)
781785
782786Since setting ` ulimit ` settings in a container requires extra privileges not
0 commit comments