Understanding Networking


I am searching for a compose configuration, but I didn’t found, what I was looking for:

I want 2 containers, lets say an app and its database. The app container must have network access to the host network (i.e. host as network_mode) and access to the database container (e.g. via bridge as network_mode). Currently, my database container exposes its port to the host, where the app container is able to connect, but I consider this as a workaround/dirty hack.

What would be an alternative configuration?

Thanks, ManOki

If you start them from the same compose file they can connect to each other using their service names, without additional configuration.
Why does the app need access to the host network? Containers should be isolated as much as possible.

I’m using a container for fhem, a house automation server, which needs to connect to other devices in my network. I don’t want to add rules for every connection/port/device. In this case I use docker to simplify the update procedure rather than to isolate as much as possible.

Here is my docker-compose.yml. Within the fhem container I cannot reach the database container by its hostname/servicename, e.g. ping database.

version: '3.3'
    image: fhem/fhem
    restart: unless-stopped
    network_mode: host
        - [...]
        - [...]
        - [...]
        - database
    image: mariadb
    restart: unless-stopped
        - 3307:3306
        - [...]
    env_file: ./database.env

If you just want to read information for example from a device tracker you should be able to access it at its IP address. Of course if you need multicast and such things then you have to integrate it into the host network.

Did you try to access it with the MariaDB client (on the default port)?

Do you mean, I should be able to access the device tracker from a container with bridge network_mode? How can I configure this?

I did using mysql -h database inside fhem container, result is ERROR 2005 (HY000): Unknown MySQL server host 'database' (-2).

Yes, it should work. For the start, don’t configure anything and let docker-compose manage the networks.
You can always send requests from a container to devices in the host network. For example I have a Homematic CCU at and I can access it with:

docker container run --rm debian ping

Most of the time the attempt to modify the settings manually is the reason why the networking is broken.

Thanks for the hint, I wasn’t aware of that! I tested this and it works as intented.

Unfortunately, I realized that I probably need multicast. Is there any other option than host as network_mode to send/listen multicast packets from a docker container to my host network?

Create an internal network and connect fhem and database to it, in addition to that connect fhem to the host network. Take a look at Networking in Compose and Network configuration reference. (At the moment I’ve no time to write more and I know these documentations are often a bit complicated.)