How to bind Docker image to specific IP on MAC
Published 4 years and 10 months ago, Categorized under: Code, docker, Linux, Nginx, PHP

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

docker run -p -name container1 <someimage> <somecommand>

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 , 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:

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

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

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

Share It via: