Docker Community Forums

Share and learn in the Docker community.

Does `network_mode: host` override a defined network in a docker-compose?

I have a few docker containers running on the same machine and one of them is a reverse proxy to route traffic to the others. I have an docker network that they all connect to that i created with docker network create net_name and each container uses that network in their docker compose file. I also specify a hostname attribute for each container, this allows the reverse proxy container to simply use their hostname instead of a set IP address.

One of these containers needs to be network_mode: host due to multicast DNS requirement. I noticed that this is the only network where I cannot use the hostname instead of the IP address of the machine with the container.

Does network_mode: host override a defined network in a docker-compose and make it so the hostname can’t be found by other docker containers connected to that same network?

Instead of just using the default app network, you can specify your own networks with the top-level networks key. This lets you create more complex topologies and specify custom network drivers and options. You can also use it to connect services to externally-created networks which aren’t managed by Compose.

Each service can specify what networks to connect to with the service-level networks key, which is a list of names referencing entries under the top-level networks key.

Here’s an example Compose file defining two custom networks. The proxy service is isolated from the db service, because they do not share a network in common - only app can talk to both.

version: “3”
services:

proxy:
build: ./proxy
networks:
- frontend
app:
build: ./app
networks:
- frontend
- backend
db:
image: postgres
networks:
- backend

networks:
frontend:
# Use a custom driver
driver: custom-driver-1
backend:
# Use a custom driver which takes special options
driver: custom-driver-2
driver_opts:
foo: “1”
bar: “2”
Networks can be configured with static IP addresses by setting the ipv4_address and/or ipv6_address for each attached network.

Networks can also be given a custom name (since version 3.5):

version: “3.5”
networks:
frontend:
name: custom_frontend
driver: custom-driver-1
For full details of the network configuration options available, see the following references:

Top-level networks key
Service-level networks key
Configure the default network
Instead of (or as well as) specifying your own networks, you can also change the settings of the app-wide default network by defining an entry under networks named default:

version: “3”
services:

web:
build: .
ports:
- “8000:8000”
db:
image: postgres

networks:
default:
# Use a custom driver
driver: custom-driver-1

Thanks for the example. But i don’t think it gets at my question. If one of those networks uses network_mode: host in its compose yaml, does that override any networks defined in it? I’m seeing that my home assistant server (which must use this network_mode for mDNS reasons) has a hostname that isn’t able to be seen by my other docker containers that are all on the same externally created docker network.