How to Access a Host Port from a Docker Container

When working with Docker containers, you may sometimes need a container to access a service running on the host machine—like a database, API server, or custom service on a specific port. This is a common scenario during development, testing, or local integration.

In this post, you’ll learn:

  • How networking works between containers and hosts
  • How to access a host port from within a container
  • Solutions for different operating systems

🧠 Understanding the Problem

By default, Docker containers run in isolated networks and cannot directly access localhost (or 127.0.0.1) of the host machine.

For example, if your host is running a web server on localhost:8000, running this inside a container won’t work:

curl http://localhost:8000

Why? Because localhost in the container refers to the container itself—not your host.


✅ Solution 1: Use host.docker.internal (Linux, macOS, Windows)

Docker provides a special DNS name:

host.docker.internal

This resolves to the internal IP address of the host, allowing containers to reach services running on the host system.

🔧 Example

curl http://host.docker.internal:8000

Use this inside your container to access the host’s service running on port 8000.

📌 Notes:

  • macOS and Windows support this natively.
  • For Linux, it works with Docker Desktop, but may require additional setup if you’re using Docker Engine.

🔁 Linux Alternative (Non-Docker-Desktop)

On Linux systems without Docker Desktop, host.docker.internal may not resolve. Instead, use the host’s IP address from the container’s perspective.

Step-by-step:

  1. Get the IP of the host network interface (typically eth0 or wlo1):
ip addr show
  1. Use the IP address directly in the container:
curl http://192.168.x.x:8000

Or pass it as an environment variable when running the container:

docker run -e HOST_IP=192.168.x.x myimage

🔧 Solution 2: Use Docker’s Host Network (Linux Only)

You can run the container using the host’s network stack:

docker run --network host myimage

Now, inside the container, localhost refers to the host’s network.

⚠️ Works only on Linux, not supported on Docker Desktop for Windows/macOS.


📦 Bonus: Define the Host in /etc/hosts

You can manually add a custom alias to the host machine in your container’s /etc/hosts:

docker run --add-host=myhost:192.168.x.x myimage

Then, in your app:

curl http://myhost:8000

📝 Summary

MethodOS CompatibilityNotes
host.docker.internalWindows/macOS/Linux*Easiest way with Docker Desktop
Use host IP addressAllRequires knowing the IP
--network hostLinux onlyShares full network with host
--add-host=myhost:<IP>AllCreates a custom alias

✅ Conclusion

Accessing host ports from Docker containers is simple once you understand the networking model. Use host.docker.internal for cross-platform ease, or switch to --network host or IP-based approaches on Linux.

Proper configuration ensures your containers can seamlessly integrate with local services—great for development and testing workflows.

Sharing Is Caring:

Leave a Comment