Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine host network with the default network in docker-compose

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

like image 228
Yan Sidorov Avatar asked Oct 04 '19 12:10

Yan Sidorov


People also ask

What is the default network in Docker compose?

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

Can a Docker container be part of two different networks?

You can create multiple networks with Docker and add containers to one or more networks. Containers can communicate within networks but not across networks. A container with attachments to multiple networks can connect with all of the containers on all of those networks.

What is default bridge network in Docker?

When you start Docker, a default bridge network (also called bridge ) is created automatically, and newly-started containers connect to it unless otherwise specified. You can also create user-defined custom bridge networks. User-defined bridge networks are superior to the default bridge network.


1 Answers

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
like image 89
Yan Sidorov Avatar answered Oct 19 '22 16:10

Yan Sidorov