I have problems creating a container with a custom host IP (other than “localhost” or “127.0.0.1”; e.g. “192.168.59.123”) on Linux. If using “localhost”, all works well.
Last year, I had a MacBook and had the same problem, but found a solution (I also created a topic). This year, I got a new notebook from my company: an Ubuntu 24.04. Unfortunately, my solution from last year on Mac does not work.
I found a lot of topics (and documentation), which suggests using mcvlan as a network driver would be the solution. But it does not work (or I am doing something wrong).
Could somebody help me getting it to work, please?
@deanayalon in the topic you linked just wrote in this last response that MacVLAN in Docker Desktop will not work, and it is trur.
I read in the linkeed topic that you didn’t even had the IP on the host from which you wanted to forward a port (maybe I misunderstood it). You say the solution that worked then, didn’t work now, but unless you share what you did exactly, I don’t think there is much we can say.
Please share what the error message is, or what you do to confirm what you want doesn’t work.
My solution on Mac was to create a new network in the network manager GUI (I can’t show a screenshot of it, because I no longer have a Mac). I had one network per project (because each project has its own host IP) and could easily turn them on / off per switch toggle (important, because having them on breaks the other technology, we are still using in most projects).
I tried the same on my new Ubuntu (screenshot below), but regardless of what I try, the Ubuntu is then using the new network for all internet connections; rendering me offline (another problem). I couldn’t get it to bind on to that created network, either (main problem). Both problems might be connected and could just be caused by wrong configurations. But all I tried didn’t work.
I write the host IP and port (e.g. http://192.168.59.123:1234/) into my browser and press enter. My browser says then, that there’s no connection. If I use localhost in my docker-compose.yml and go to http://localhost:1234/, then I reach the database.
So if I understand it correctly, the problem is that the request is sent to the port but the response is routed through a network which is isolated from your machine. At least I could imagine something like that. If you are not sure, you could try monitoring packages using tshark on the server for example and filter to the source IP
If you need to route the traffic from a container through a specific IP on an interface, you can check what I wrote here:
But I think the response should always be routed through the same interface so this might not be a solution for you. Checking packages with tshark could still help.