How to use host network while linking to a container?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Inter-Container Communication: How to Link Containers Without Conflicts

As developers working with Docker and container orchestration tools like Docker Compose, managing networking between services is a critical skill. When you start introducing advanced networking features, such as net: "host", conflicts often arise, especially when trying to combine this with standard linking mechanisms like links or service discovery.

This post dives into the specific conflict you encountered—trying to use host networking while linking containers—and provides a robust, developer-approved solution for ensuring your Laravel application can reliably communicate with its PostgreSQL database container (postgis).

The Networking Conflict Explained

The error message you received: Conflicting options: host type networking can't be used with links. This would result in undefined behavior, is not an arbitrary error; it highlights a fundamental conflict in how Docker manages network isolation and service discovery.

  1. net: "host" Mode: When you set a container to use the host network mode, it bypasses Docker’s internal virtual bridge network and gains direct access to the host machine's entire network stack. This is powerful for performance-critical applications (like your PubNub client needing high-performance I/O).
  2. links / Service Discovery: The links directive in Docker Compose relies on Docker’s internal networking system to establish communication between containers using service names.

These two modes operate on fundamentally different network layers. Using net: "host" breaks the predictable, container-to-container relationship that links expects, leading to undefined and unstable behavior.

The Recommended Solution: Embrace Service Discovery

For most applications, including those built around Laravel where services need to communicate (like an application server needing a database), the safest, most portable, and most scalable approach is to leverage Docker's default bridge networking and service discovery. This method ensures that containers communicate via well-defined internal DNS names rather than direct host manipulation.

Instead of forcing net: "host", we should focus on configuring the ports correctly so the services can talk securely within the Docker network.

Refactoring for Reliable Linking

We will remove the conflicting net: "host" setting and rely solely on defining internal container networks and exposing only necessary ports. The Laravel application will connect to the database using the service name defined in docker-compose.yml.

Here is how you should refactor your setup:

yaml
version: '3.8'

services:
  laravel:
    image: trackware
    # Remove net: "host" to resolve the conflict
    links:
        - postgis
    ports:
        - "80:80"
        - "3306:3306" # Expose DB port if needed externally, but internal is key
        - "443:443"
        - "220:22"
        - "8000:8000"
    # The service name 'postgis' becomes the hostname for linking
    restart: always
    volumes:
        - C:/H/repositories/pubnub:/share
    container_name: laravel

  postgis:
    image: mdillon/postgis
    env_file: .postgis_env
    # Expose only the required database port internally
    ports:
        - "54320:5432" # Map internal port to host (optional, depends on external access needs)
    container_name: postgis

Connecting Laravel to PostGIS

With this structure, the laravel container can reach the postgis container simply by using its service name (postgis) as the hostname and the internal port it exposes. In your Laravel application's configuration (e.g., .env file), you would configure the database connection string like this:

dotenv
DB_HOST=postgis
DB_PORT=5432 # Or whatever internal port PostGIS is listening on
# ... other credentials

This approach aligns perfectly with container best practices, promoting decoupling and making your deployment easier to manage, which is a core principle encouraged by platforms like Laravel Company.

When Host Networking Is Necessary (Advanced Context)

If the PubNub client truly requires extremely low-latency, direct access to specific host network interfaces that Docker's bridge networking cannot provide—and you have thoroughly benchmarked that this is necessary—you must treat the container as a privileged entity. In such rare, high-performance scenarios, instead of relying on links, you would use Docker’s --network=host flag directly when running the main application container.

However, for standard application linking, sticking to service discovery (as shown above) is the correct architectural choice. It keeps your system maintainable and avoids the unpredictable behavior of mixing host networking with internal link management.

Conclusion

The key takeaway here is that conflicts in Docker Compose often arise from attempting to mix high-level abstraction features (links) with low-level, system-specific configurations (net: "host"). For reliable containerized applications, always favor the well-defined service discovery provided by Docker's bridge network. By refactoring your setup to use service names for linking, you ensure that your Laravel application and PostgreSQL database communicate securely and reliably, setting a strong foundation for scalable development.