Localhost inside a Container

I’m currently running two web servers in my resin container. One to setup a WIFI hotspot with a local UI for networking, the other to handle the actual network communications. In order to communicate between them I had planed on using localhost. My experience so far is that this won’t work for the webservers. I found the following post https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach that says this will not be possible with how Resin is setup by default. The oddest thing is that when I SSH into my device I can run curl commands to localhost and they work.

In order to make localhost work we need to run with “–net=“host”” but that command is handled behind the scenes and instead uses the default bridge network. Are there any solutions that have worked for others? I have not found a way to run Resin with the host networking setup, is there a way?

I’m a networking novice, but my current plan is to attack this by trying to figure out the address of the bridge network, and use that as a loopback instead of localhost. Any thoughts or comments would be greatly appreciated.

I’m not sure I totally understand how you are planning to use the localhost. Actually, I assume that you mean the host OS probably. I assume that if you know the IP of the docker container, each web server can communicate with the other, right? Unless I’m missing something here.
Also, regarding the WIFI hotspot, perhaps you would like to take a look at our docs:

You can read more on resin networking here: https://docs.resin.io/deployment/network/2.x/#introduction

I had planned on using localhost as the IP address of my two webservers so that they could communicate with each other. For example the network manager would be able to send HTTP requests to localhost:80 to talk to the front end server. The front end server would be able to send back to localhost:8080 to talk to the network manager.

Based off of your response, it seems like I shouldn’t be using local host or the docker bridge (as I was going to try), instead my docker container has an IP address that both of my Web servers will have. So if I can figure out my docker container IP address, I can use that. Is that correct?

Thank you for your response.


PS. We have looked at the hotspot docks quite a bit, they have been very helpful. Thanks :slight_smile:

You can have the one server listen at port 80, and the other server listen at port 8080. If you send requests to the device IP (that you see in the dashboard) you will be able to communicate with the different servers.

Resin.io devices expose all ports by default, meaning you can run applications which listen on any port without issue. There is no need to have the Docker EXPOSE command in your Dockerfile.

Also, with regards to web forwarding, resin.io currently exposes port 80. To enable web forwarding on a specific device, navigate to the device’s actions tab on the resin.io dashboard and select the Enable a public URL for this device checkbox.

Hope the above info helps!

Is there an easy programatic way to get that information? We will have hundreds of devices out in the field and can’t set this manually. Our use case is very similar to WIFI - Connect. We will have device will go offline for an extended period of time. Then come back online with a new IP address. If we could get the IP address programmatically we should be in the clear.

If I understand it correctly and you’d like to programmatically enable the the device URLs, you can use our CLI tool:

It has a command to enable the public URL with the resin device public-url enable <uuid> command, and you can script this (getting the UUIDs from resin devices. The URL will be https://<full-uuid>.resindevice.io for each of the devices.

Alternatively you can use the resin SDK to have all of these functionality and more.

I am not trying to enable the public device URL. I’m really looking for a host name that I can use in my application instead of localhost.

My understanding is:

  1. localhost is typically used to loop messages back to yourself.
  2. I have two servers that want to talk to each other in the same docker container.
  3. Because we are in a docker container we can not use localhost as that references the host device.
  4. If I was not using a docker container I could accomplish what I want with localhost. I need an alternative.

Does that make sense? I feel like i’m not communicating well.

I understand that if I knew my IP address I could use that, but I currently lack a good way to get that automatically. I do not believe that enabling a public URL would help with this. I may be wrong.

Yeah, sorry, it makes sense, I think I misunderstood some of the earlier thread. Looking at options.

Thank you, I appreciate it.

Hey, have you actually tried out whether localhost is not working in the Docker container?

I’ve put together a quick test, running two servers in a container, one on port 8000, another on 9000, when either of them receives a query, after a delay it will query the other on localhost (and kick-starting it by one of of the servers)

If I run that on a resin device, it works as expected…


Am I missing something again? It seems to me that there’s no issue using localhost within the same container.

Thats odd to me. We do have two servers in the same container failing to talk… I’m going to investigate our specific use case a lot closer. I’ll be sure to report back.

Reporting back. We had an issue in our server setup and use. Thank you for all of your assistance!

1 Like

I have a similar scenario, I’m wondering what this solution here was? I have two containers, one which is a express web server and the other which is my mongodb. I wanted the web server to talk to the mongo container using localhost, but that didn’t work. I can use the IP I see in my dashboard, that works fine, but how to I programmatically get that IP instead of hardcoding it?

UPDATE: Found my anwser, so simple. Docker Networking