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

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


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


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.


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