53

I want to connect two Docker containers, defined in a Docker-Compose file to each other (app and db). And one of them (app) should also be connected to the host network.

The containers should be connected to a common user-defined network (appnet or default) to use the embedded DNS capabilities from docker networking.

app needs also to be directly connected to the host network to receive ethernet broadcasts (network layer 2) in the physical network of the docker host.

Using both directives network_mode: host and networks in compose together, results in the following error:

ERROR: 'network_mode' and 'networks' cannot be combined

Specifying the network name host in the service without defining it in networks (because it already exists), results in:

ERROR: Service "app" uses an undefined network "host"

Next try: define both networks explicitly and do not use the network_mode: host attribute at service level.

version: '3'
services:

  app:
    build: .
    image: app
    container_name: app
    environment:
      - MONGODB_HOST=db
    depends_on:
      - db
    networks:
      - appnet
      - hostnet

  db:
    image: 'mongo:latest'
    container_name: db
    networks:
      - appnet

networks:
  appnet: null
  hostnet:
    external:
      name: host

The foregoing compose file produces an error:

ERROR: for app network-scoped alias is supported only for containers in user defined networks

How to use the host network, and any other user-defined network (or the default) together in Docker-Compose?

Simon Schürg
  • 2,134
  • 2
  • 20
  • 31

3 Answers3

18

TL;DR you can't. The host networking turns off the docker network namespace for that container. You can't have it both on and off at the same time.

Instead, connect to your database with a published port, or a unix socket that you can share as a volume. E.g. here's how to publish the port:

version: "3.3"

services:

  app:
    build: .
    image: app
    container_name: app
    environment:
      - MONGODB_HOST=127.0.0.1

  db:
    image: mongo:latest
    container_name: db
    ports:
      - "127.0.0.1:27017:27017"
ErikE
  • 48,881
  • 23
  • 151
  • 196
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 1
    This is a fantastic answer, and majorly overlooked. Published port gives the best of both worlds, with observability on localhost as well as private networking where the container services can resolve each other by container name. Unfortunate reality is that you can't route packets into Docker for Mac from the host OS, but you can use published port to connect the containers to each other and the host OS. – Kingdon Feb 21 '20 at 02:11
  • This answer saved me. I'm using Docker Swarm so used this answer to do something similar. The container I put on the host network, I also added extra_hosts: 0.0.0.0:portNumber:PortNumber – Martin Mar 19 '22 at 19:06
0

To use host network, you don't need to define it. Just use "ports" keyword to define, which port(s) from service you want to expose in host network.

Miq
  • 3,931
  • 2
  • 18
  • 32
  • `app` needs OSI layer 2 access to the host network. I am not asking for UDP/TCP port bindings. – Simon Schürg Apr 04 '18 at 17:57
  • 1
    Sorry, my bad. I did reproduce it on latest docker for windows - same issue. very similar issue is on moby github - https://github.com/moby/moby/issues/32957 - seems that this is a part of a bigger problem – Miq Apr 04 '18 at 21:00
  • 1
    Yes, I think the only way to use a multi container app with docker compose and host-networking is, to use host-network for all containers in the compose file. At least with the current version of Docker Compose of January 2018. – Simon Schürg Apr 05 '18 at 17:54
  • this answer helped me in a way that I removed the bridge network from container that had to use it, used `ports` directive for this container with internal networks related to other containers and also used `expose` for those other containers - it works like a charm – tymik Aug 20 '20 at 21:26
0

Since Docker 18.03+ one can use host.docker.internal to access your host from within your containers. No need to add host network or mix it with the user defined networks.

Source: Docker Tip #65: Get Your Docker Host's IP Address from in a Container

yerlilbilgin
  • 3,041
  • 2
  • 26
  • 21