--add-host=host.docker.internal:host-gateway

The host has a changing IP address (or none if you have no network access). We recommend that you connect to the special DNS name host.docker.internal which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows / Mac.

You can also reach the gateway using gateway.docker.internal.

docker -v
Docker version 20.10.8, build 3967b7d
spring run helloWorld.groovy
docker run -it ubuntu bash
apt update & apt install curl -y
curl http://host.docker.internal:8080
root@8024db7c38fe:/# ping host.docker.internal
ping: host.docker.internal: Name or service not known
--add-host=host.docker.internal:host-gateway
docker run -it --add-host=host.docker.internal:host-gateway ubuntu bash
root@c1d299ee554d:/# curl http://host.docker.internal:8080
Hello world container. You are able to reach the Docker host!
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐