Docker
macOS
networking
IP address
containerization

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.

Practice system design

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:

bash
curl http://host.docker.internal:3000/health

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:

python
1import requests
2
3response = requests.get("http://host.docker.internal:3000/health", timeout=3)
4print(response.status_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:

bash
getent hosts host.docker.internal

Or with Python:

python
import socket

print(socket.gethostbyname("host.docker.internal"))

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:

bash
curl http://localhost:3000/health

Then verify it from the container:

bash
docker run --rm curlimages/curl curl -v http://host.docker.internal:3000/health

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.internal instead 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.