I need to get local ip of host machine on every container startup (nodejs api)

Hello. The small company I am working for is developing an IoT app that should end the need for 10000 aps for your smart devices.

I need to dockerize my expressJs API(s). We have a cloud that is used for login and remote access only. The api that does the whole work it will be on a local hub. We will offer hardware and software versions of our app.

For the software version the local api is a container based on nodejs. Database is a mongoDB. I created a simple docker-compose that starts the mongodb container and our hub container.

I need to be able to get host local ip address every time the docker container starts, in order to send updates to the cloud. The app will be able to detect if you are on the local network, so you can use your local smart devices even if your internet connetion is down, as long as you are on same local network with the hub.

For that, every time the api starts, I post an update of the local ip of the hub to the cloud, so you know the right local ip on every project (hub) of yours.

The only way I managed to do it for every platform is having a python script that gets the local ip and replaces it in docker-compose > service > environments.

I am pretty new to docker, i’m kind of a one man army and have no one to ask for guidance, and I’m commited to do the job in the proper way.

Other then a few basic demos at college, I did not use docker.

How I can pass the local ip of the host machine to the nodejs app, so it can send it securely to the cloud.

The other option is to do this part outside of docker container, but I dont like the extra steps for the ease of use.

Thank you for your time, I hope this is possible.

Let me ask the obvious: have you considered the container with --network=host mode, to make it share the network namespace without any isolation?

So you tell me that with this simple solution, i would be able to access the container from any device on the local network via the container’s ip? That sound great! Gonna try it out, thanks.

This is literally my first real use of docker, other then university projects. Did not know this.

Edit: I tried and found out that this does not work on docker desktop for windows. So this will work on linux but not on mac or windows. If there is no other solution i will have to limit supported operating systems to linux only.

True ,it does not! It would have been helpful if you’d have mentioned in the first post that you need a solution for Docker Desktop as well. Though, you mentioned IoT, which does not sound like a platfrom where people would run Docker Desktop on.

Almost correct. This will not work for any Docker Desktop variation, as all of them have in common that they use a utility vm. Of course using the host network of the utility vm for a container will not give you the real hosts network interface.

Would it help if you use an environment variable and pass the full qualified domain name of the host into the container and resolve the ip from inside?

That sounds actually interesting.
I will try it, and write back.

Yes, IoT users are pretty familiar with linux server and VM’s, but we are here to provide features like home assistant and more then it for people with no IT knowledge, closer to “magic apple experience”. You install one thing on your old laptop, that is a hub with build-in UPS, reduce e-waste and have much more resources then any IoT hub, even better then our hardware solution, that will enable to use the IoT hub to process locally multiple rtsp camera streams, instead of one or two.