Search code examples
macosdockerhost

Docker Mac alternative to --net=host


According to the docker documentation here

https://docs.docker.com/network/host/

The host networking driver only works on Linux hosts, and is not supported on Docker for Mac, Docker for Windows, or Docker EE for Windows Server.

On Mac what alternatives do people use?

My scenario

  1. I want to run a docker container that'll host a micro-service
  2. The micro-service has dependencies upon databases that I'm also running via docker
  3. I thought I'd be able to use --net=host on Mac when running the micro-service
  4. But the micro-service port is not exposed
  5. I can override the db addresses (they default to localhost) on the microservice.
  6. But that involves robust --env usage

What's the simplest / most elegant solution?


Solution

  • The most simple and most elegant solution is to use docker named bridge network. You can create a custom bridge network (default is bridge) like this:

    docker network create my-network
    

    Every container deployed inside this network can communicate with each other by using the container name.

    $ docker run --network=my-network --name my-app ...
    $ docker run --network=my-network --name my-database...
    

    In the example above you can connect to your database from inside your application by using my-database:port. If the container port is exposed in the Dockerfile you don't need to map it on your host and you can keep all your communication internal inside your custom docker bridge network.

    In most cases the application its port is mapped (example: -p 80:80) so localhost:80 is mapped on container:80 and you can access the app from on your localhost. If the app needs to communicate with a db you don't need to expose the port of the db and you don't have to map it on localhost as explained already above. Just keep the communication between app and db internal in your custom bridge network.