Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 926
Simon Schürg Avatar asked Nov 15 '17 09:11

Simon Schürg


People also ask

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 the difference between bridge and host network in Docker?

Bridge networks are usually used when your applications run in standalone containers that need to communicate. See bridge networks. host : For standalone containers, remove network isolation between the container and the Docker host, and use the host's networking directly.


1 Answers

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 
like image 120
BMitch Avatar answered Oct 14 '22 20:10

BMitch