Docker localhost

Today, I was trying to transit my golang webserver project into docker. When testing in my local machine, I couldn't connect to the the docker container by any means. The postman kept saying "Error: socket hang up", it means the docker router service is up, but we just can't connect into the container.

There were no error logs in the container, nothing. Thankfully, I accidentally found this answer. It suggests using 0.0.0.0 instead of localhost for the webserver's setting. It actually worked!

By changing my configuration from localhost:8080 to 0.0.0.0:8080, and then run docker run -p 8080:8080 myImage, the server can response correctly.

But why

When the server listens on localhost:8080, it's only available on the loopback network interface – that is, it can only accept connections from the same host. localhost usually maps to the IP address 127.0.0.1, which is the loopback address. This is useful for testing and development, but it means that other machines (including Docker containers) can't connect to the server.

On the other hand, when the server listens on 0.0.0.0:8080, it's available on all network interfaces. 0.0.0.0 essentially means "all IP addresses on the local machine" (or more technically, "all available network interfaces"). So, when running in a Docker container, it can accept connections from any IP address that can route to the Docker container, including from the host machine or other Docker containers.

So, by changing my server to listen on 0.0.0.0:8080, it becomes available not just to the the container it reside, but also to the other container, docker routers and outer world. Through the picture, we can also see the log of the server. The request is coming from a LAN IP address (x.x.0.1 usually is the router address I guess)

And oh, one more thing. The reason why I bold the network interface above is that I think this is a critical concept to this problem and gave a great enlightening to me.

Well, what a journey!

 

By bruce

Related Post

Leave a Reply

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