2019-11-08
How to combine host network with the default network in docker-compose
stackoverflow
Question

I'm building the docker-compose service that includes two containers. One of those containers (node) is designed to support an auto-discovery mechanism and needs to be a part of the host LAN (as I need multicast UDP packages to be handled by the LAN router, not the built-in docker router).

While the "network_mode: host" in docker-compose.yml perfectly does the trick, I need this service to be also available to a second container (qtcore) by its hostname via the default docker-compose network (like that: http://node:37326). And that doesn't seem to be possible with the network_mode set to "host".

My docker-compose.yml currently looks like this:

version: '3.7'
services:
    qtcore:
        image: yansidorovtesseris/qtcore
        build: .
        container_name: qtcore
        depends_on:
            - node
        env_file: defaults.env
        ports:
            - "8000:8000"

    node:
        image: yansidorovtesseris/komodo
        container_name: node
        env_file: node.env
        ports:
            - "37326:37326"
            - "1900:1900"
        network_mode: host
        volumes:
            - $HOME/node_state:/komodo/.komodo/$AC_NAME

I've tried to use the sample from the docker-compose docs (https://docs.docker.com/compose/compose-file/#host-or-none) to connect the host network as an external network. With the thought to add both host and default networks to a service.

version: '3.7'
services:
    node:
        ...
        networks:
            hostnet: {}
        ...
networks:
    hostnet:
        external: true
        name: host

But all I get when I try to run the docker-compose is the following error: ERROR: for node network-scoped alias is supported only for containers in user defined networks

Answer
1

Alright, it seems to me that it's impossible to configure service to use both "host" and "bridge" options simultaneously. Would be glad if someone points out the opposite, though.
I've come up with a simple workaround to allow services to operate as a one docker-compose unit in a host LAN and yet use the "extra_hosts" (suggested by hNczy) for the hostname lookup.
It isn't ideal and might not fit for every scenario, but it does the trick for me.
Basically, two services are both using the "network_mode: host" right now, and the "extra_hosts" of a "qtcore" service is supplied with the "node" name bound to 127.0.0.1.

version: '3.7'
services:
    qtcore:
        image: yansidorovtesseris/qtcore
        build: .
        container_name: qtcore
        depends_on:
            - node
        env_file: defaults.env
        network_mode: host
        extra_hosts:
            - "node:127.0.0.1"
        ports:
            - "8000:8000"

    node:
        image: yansidorovtesseris/komodo
        container_name: node
        env_file: node.env
        ports:
            - "37326:37326"
            - "1900:1900"
        network_mode: host
        volumes:
            - $HOME/node_state:/komodo/.komodo/$AC_NAME
How to combine host network with the default network in docker-compose
See more ...