How to get mac host IP address from a docker container?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
On Docker Desktop for macOS, the most practical way for a container to reach the host is usually not by hardcoding an IP address at all. Use the special DNS name host.docker.internal instead, because the actual host-side address is part of Docker Desktop’s VM networking and can change.
Why macOS Is Different from Native Linux
On Linux, containers often interact with the host through bridge networking concepts that feel more direct. On macOS, Docker Desktop runs containers inside a lightweight Linux VM, so the networking path between container and host is different.
That means common Linux assumptions such as “the host is always on a fixed bridge IP” are not reliable on a Mac. If you hardcode one of the well-known Docker Desktop addresses, the setup may work today and break later.
Use host.docker.internal
The recommended pattern is simply to talk to the host by name:
If your Mac is running a local service on port 3000, that DNS name lets the container reach it without caring about the exact IP.
The same idea works in application code:
This is usually better than asking for the raw IP, because the name is stable even when the underlying address is not.
Resolve the IP Only If You Truly Need It
Sometimes you really do need the numeric address for debugging or for a tool that does not accept hostnames. In that case, resolve the special hostname from inside the container:
Or with Python:
This gives you the current address as seen by the container. The important part is that you discovered it dynamically instead of hardcoding it.
Make Sure the Host Service Is Reachable
If the hostname resolves but requests still fail, the next problem is usually not IP discovery. It is one of these:
- The host service is not running
- The host service is bound only to the wrong interface
- A local firewall is blocking the port
- You are using the wrong port
For example, if a service is supposed to be reachable from the container, verify it from the Mac first:
Then verify it from the container:
That sequence quickly separates “container cannot find host” from “host service is not actually listening.”
Why --network host Is Not the Answer
Many Linux Docker examples rely on host networking. That is usually not the right answer on Docker Desktop for macOS because the runtime architecture is different. Even when similar options exist, they do not mean “share the exact Mac host network stack” in the same direct way they do on native Linux.
For everyday local development, host.docker.internal is the cleaner and more portable approach.
Common Pitfalls
The most common mistake is hardcoding a Docker Desktop gateway IP such as 192.168.x.x and treating it as permanent. Those values are implementation details, not stable application contracts.
Another pitfall is assuming a service listening on the Mac is automatically reachable from the container. If the process is down, misbound, or blocked by a firewall, the hostname alone will not save you.
It is also easy to copy Linux-focused Docker networking advice into a macOS setup without noticing that Docker Desktop uses a VM boundary. That leads to a lot of confusing but avoidable debugging.
Finally, do not resolve the numeric IP unless you need it. The special hostname is usually the better interface because it stays stable while the underlying address may not.
Summary
- On macOS, prefer
host.docker.internalinstead of hardcoding a host IP. - Docker Desktop networking goes through a VM, so Linux bridge assumptions do not map directly.
- Resolve the hostname dynamically only if you truly need the numeric address.
- Verify that the host service is actually running and reachable on the expected port.
- Treat fixed Docker Desktop gateway IPs as unstable implementation details, not configuration values.
Related reading
- How to get pipenv running in docker?
- How to get the IP address of the docker host from inside a docker container
- How to get the list of dependent child images in Docker?
- How to get the status of a particular pod or container kubectl get pods using jsonpath
- How to get my IP address programmatically on iOS/macOS?
- How to get Python requests to trust a self signed SSL certificate?
- How to handle database migrations with Kubernetes and Skaffold
- How to improve random number generation in kubernetes cluster containers?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.