jbarros
jbarros

Reputation: 638

Docker Compose networking: Bridge driver with access to host machine

Is there any way to have a microservices architecture in Docker, using Docker Compose, in such a way that the communication between containers is with the default bridge value driver (i.e. I didn't specify any network_mode in any service in docker-compose.yml), but all the containers having access to a common service in the host machine?

Thank you in advance.

Upvotes: 1

Views: 3170

Answers (1)

larsks
larsks

Reputation: 311605

Your containers always have access to your host machine.

Under Linux, you can just use the address of the default gateway inside the container; this will be the address of the bridge on your host to which your container is attached. Any host services that are listening on all interfaces will be available at this address. Assuming your container has the iproute package installed, you could get the gateway address using something like:

ip route | awk '$1 == "default" {print $3}'

You could of course use the address of any host interface and pass that it as an environment variable (docker run -e HOSTADDR=192.168.23.5 ...); using the gateway address is convenient because it is discoverable from inside the container.

Under Docker for MacOS or Docker for Windows, you can use the special hostname host.docker.internal to refer to the host, as documented for example here.

Note that if your host service is not listening on all interfaces or if you have a restrictive firewall configuration, you may need to make changes before the above will work.

Upvotes: 3

Related Questions