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

╄→гoц情女王★ 提交于 2019-12-02 22:34:25

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!