Docker: get host ip from container
Host IP
To get the host’s IP from a container:
- in
MacOSandWindows, just use the host:host.docker.internal. If you ping this host, you will be able to see the host ip. - in
Linux, inside container, run/sbin/ip route|awk '/default/ { print $3 }'and usually this print ip:172.17.0.1
docker-compose file
So in your docker-compose.yml file, if you want to connect to your host’s redis:
1 | services: |
The REDIS_SERVER env var indicates that if the env var DOCKER_HOST exists, if it exists, use that value, otherwise, use string host.docker.internal .
So in Linux before we do docker-compose, we can set the DOCKER_HOST env var:
1 | $ export DOCKER_HOST=172.17.0.1 && docker-compose |
In MacOS and Windows, we don’t need to set this variable, and can just do:
1 | $ docker-compose |
Other: check docker network interface
- List all network interface:
docker network ls - Inspect a network interface: e.g.
docker network inspect bridge,docker network inspect host