Docker
Network Host
Application Ports
Troubleshooting
Connectivity Issues

Docker with --network host does not allow to access to port of application

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Docker is a powerful tool for containerization, allowing developers to package applications and their dependencies into a standardized unit for software development. The --network host option is a feature in Docker that is often misunderstood, especially when users attempt to access applications exposed on specific ports.

When running a container with the --network host option, Docker is instructed to share the host's networking namespace. In simpler terms, it is as if the container is directly using the host's network stack without any Docker-specific abstractions. This feature can be both advantageous and challenging depending on the context in which it's used.

How --network host

Works

Using the --network host option means that the container will be able to use the networking setup of the host directly. This includes access to all available network interfaces and address configurations.

Technical Explanation

  1. Direct Network Access: The container bypasses Docker's user-defined bridge network, allowing it to listen on the host's IP address and open ports directly on the host.
  2. No Port Binding Required: Normally, you would use -p or --publish to bind container ports to the host. With --network host , this step is unnecessary because the container's ports are the same as the host's.
  3. Reduced Isolation: The separation between the host and the container's network is diminished. This might introduce security concerns since the container can potentially access services intended only for the host or manipulate network traffic.

Common Issues and Misconceptions

Despite its benefits, using --network host can introduce issues, one of which is the application ports not being accessible as expected. Let's explore several reasons why this might happen:

1. Port Conflicts

When a container is sharing the host's network stack, a conflict can arise if the host already has services running on the same ports. For instance, if a web server is running on port 80 on the host, a containerized service attempting to use the same port will fail to start or be inaccessible due to this conflict.

2. Firewall Rules

Firewalls configured on the host may block access to certain ports. Since the container's networking relies on the host's configuration, these rules apply equally to the service inside the container.

Example:

  • Network-specific Testing: Containers that require testing under the same conditions as the host network, such as network diagnostic tools.
  • High-performance Applications: Applications that demand the least network overhead possible.
  • Security: Always evaluate the security implications when reducing network isolation with --network host .
  • Application Configuration: Configure applications to bind to the correct IP addresses and ports.
  • Testing and Documentation: Before deploying, test configurations in a safe environment and document all network requirements.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design