Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Mac alternative to --net=host

Tags:

docker

macos

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?

like image 495
Shane Gannon Avatar asked Sep 28 '18 12:09

Shane Gannon


People also ask

Can Mac run Docker natively?

Docker can't run natively since it uses linux kernel features not available on a mac.

Can you run Docker on Mac without Docker desktop?

UPDATE 2022-06-28: I'm still using this setup. Works amazingly. Haven't thought about Docker Desktop until someone gilded me for this post.

Is Mac good for Docker?

Here's a quick summary with the best benefits of using Docker for Mac: Pretty smooth and simple installation process. No more VirtualBox! No problems with virtual machines, filesystems or other bugs you might have encountered with the Docker Toolbox.

Does Docker need VirtualBox on Mac?

You can run both HyperKit and Oracle VirtualBox on the same system. To learn more, see Docker for Mac vs. Docker Toolbox. So it seems like if you are not concerned about docker-machine create then you can use Docker CE without VirtualBox installed.


1 Answers

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.

like image 167
lvthillo Avatar answered Sep 23 '22 13:09

lvthillo