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

Leave a Comment