How to access host port from docker container
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Accessing a host port from a Docker container can be a common requirement when you want your container to interact with services running on your host machine. There are a few ways to accomplish this, depending on your use case and Docker setup. This article will cover the steps and provide examples for accessing a host port from within a Docker container, helping you integrate your containers with other applications on your host system effectively.
Understanding the Docker Network
Before diving into how to access host ports, it's crucial to understand Docker's networking model. By default, Docker containers operate in an isolated network environment. This means that they don't communicate with the host network directly unless explicitly configured to do so. This isolation is beneficial for security and consistency but requires specific configurations to allow communication between the host and containers.
Methods to Access Host Ports
Method 1: Host Network Mode
The simplest way to allow a container to access host ports is by using the host network mode. In this mode, your container shares the host's network stack, which makes any port on the host directly accessible from the container.
Example:
Suppose you want to allow a Docker container to access RabbitMQ running on port 5672 on the host machine. You can run a container using the host network mode as shown below:
Inside the container, you can now connect to localhost:5672 as if you were on the host machine.
Key Points:
- Up-side: Simplicity; no need for additional configuration.
- Down-side: Potentially less secure as containers share the same network namespace as the host.
- Use-case: Suitable for development and situations where security isolation is not a concern.
Method 2: host.docker.internal
Docker provides a special DNS name host.docker.internal which resolves to the host IP from within the container. This feature is available on Docker for Windows and macOS and supports the communication with the host for specific use-cases.
Example:
For a service running on port 8000 on the host:
Inside the container, access the host service:
Key Points:
- Up-side: Does not require network stack sharing.
- Down-side: Limited to Docker Desktop for Windows and macOS.
- Use-case: Good for cross-operating system development environments.
Method 3: Port Mapping
Port mapping is more commonly used for the inverse scenario — exposing a container's port to the host. However, you can configure the Docker network in a way that allows containers to communicate with the host using specific IP addresses or named interfaces.
Example:
To expose a host service to a container, first, ensure the service binds to all available network interfaces or the Docker bridge interface.
For a host service on port 8080, configure Docker to expose it through a mapped interface. For example, you might use:
The above example exposes the host's port 8080 to the container's port 3000, achieving the desired connectivity if both the host service and container are appropriately configured.
Key Points:
- Up-side: Fine-grained control over service exposure.
- Down-side: Requires careful configuration of port bindings.
- Use-case: Ideal for developing, testing, or exposing specific services in a controlled manner.
Additional Considerations
- Firewall and Security: Ensure proper firewall rules are in place on the host to allow communication on the necessary ports.
- Service Binding: Ensure the host service is listening on appropriate interfaces (
0.0.0.0or specific network interfaces) to accept connections. - Network Bridge: In more complex architectures, you can utilize Docker's custom bridge networks to set up intra-network communication where containers and host services cooperate seamlessly.
Summary
| Method | Description | Pros | Cons | Use-case |
| Host Network Mode | Shares the host's network stack. | Simple setup | Reduced isolation | Development and non-sensitive environments |
host.docker.internal | Special DNS for host access (Docker Desktop). | Easier access in cross-platform environments | Limited to Docker Desktop | Multi-platform development |
| Port Mapping | Maps specific host ports to container ports. | Fine-grained control | Requires manual configuration | Development, testing, controlled exposure |
By utilizing these methods, you can enable your Docker containers to effectively communicate with host services, maximizing the utility of your layered application architecture. Choose the method that best meets your security and development needs to ensure a robust and flexible Docker environment.
Related reading
- How to access host's localhost from inside kubernetes cluster
- How to access NodePort in Minikube with docker driver?
- How to access private Docker Hub repository from Kubernetes on Vagrant
- How to add a Container View programmatically
- How to access hosts in my network from microk8s deployment pods
- How to access HTTP headers for request to AWS API Gateway using Lambda?
- How to analyze a java thread dump?
- How to analyze logs in a distributed system?

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.