How can I use the host network in Docker Compose?

I have a Docker container that needs to access a local REST API at http://127.0.0.1:8080. Using docker run --net=host works fine, but when I try to scale the container with Docker Compose, the container cannot reach the API. My Compose file uses:

version: "3"
services:
  web:
    image: containera:latest
    network_mode: "host"
    deploy:
      replicas: 1

However, I get Ignoring unsupported options: network_mode. How can I configure docker compose network host properly to access the host network?

Hey! I’ve hit this exact issue :sweat_smile:. The key is that network_mode: "host" works only in single-host mode and is not supported with Docker Swarm’s deploy options, like replicas.

Example for a local Compose file:

version: "3.8"
services:
  web:
    image: containera:latest
    network_mode: "host"

Do not include deploy.replicas if you want host networking.

On Linux, your container can now access 127.0.0.1:8080 on the host.

Works perfectly for development and local testing.

Why deploy.replicas breaks host networking

  • I ran into this myself and it was confusing. The reason:

  • deploy is a Swarm-only key, used for scaling services.

  • Swarm doesn’t support host networking for replicated services.

  • That’s why you see Ignoring unsupported options: network_mode.

:bulb: My tip: If you want multiple replicas, you’ll need another networking strategy, not host mode.

If you need scaled containers that still reach the host API, you can’t rely on host networking. Instead:

Use the default bridge network.

Reference the host machine by its real IP (not 127.0.0.1):

version: "3.8"
services:
  web:
    image: containera:latest
    networks:
      - webnet

networks:
  webnet:
    driver: bridge
  • On Linux, the host IP can be 172.17.0.1 (Docker gateway) or your real LAN IP.

  • In the container: http://172.17.0.1:8080 instead of 127.0.0.1.

This approach works with scaling (replicas > 1) and avoids host network limitations