How to bind Docker image to specific IP on MAC

PS: this is an experimental idea so it might not work for you.

I’ll try to make short here, so basically you will have to pass the IP with the expose port while you execute docker run for example something like

Now the question why do I need to do something like that since at the end docker will be bind to localhost and more specific to 0.0.0.0 , and to be honest this is just a simple try for me to imitate the ability to have two (or more) vagrant boxes running on the same machine and both has the port 80 exposed to the host.

So normally you will only have one image cause you cant have two images on the same port (or at least this is what I have in my mind right now), so the command above will help, now when you try it you will face a small problem, which is that the IP should be the same as your host IP and you cant have both images have the same IP, so it wont work and we have gain nothing, lets talk about how vagrant/virtualbox work, they do a small trick where virtualbox will create a virtual network adapter and configure it to have the IP which you specified in Vagrantfile, so I guess we will have to do the same Creating a new virtual network adapter.

On mac its easy to create such adapter, just go to System Preferences -> Network and from there you can add a new adapter like the following:

network-1

after adding the network adapter you will need to specify some default information like: IP, Subnet and Router something like the following image

network-2

Now after you add them you can use this IP in your docker run command or even much more fun you can use it within your docker-compose.yml file like

network-3

 

Let me know what you think, and if it worked for you or not.

Filed under: Code, docker, Linux, Nginx, PHP

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *

Name *
Email *
Website

19 + twelve =