0

When starting a gin server, I was using router.Run("localhost:8080").

Afterwards, running the docker command docker run -p 8080:8080 <IMAGE_NAME> does not expose the service to my local machine.

It only worked after I changed it to router.Run("0.0.0.0:8080"). Is there a difference between localhost and 0.0.0.0? If so, what is the difference in context of a docker container?

I am expecting that localhost is the same as 0.0.0.0 as that is the behaviour on my local machine during development.

My main function is below

func main() {
    fmt.Println("hello world")

    router := gin.Default()

    router.Use(middlewares.CORSMiddleware())
    router.POST("/api/notify", notify.NotifyHandler)

    // router.Run("localhost:8080")
    router.Run("0.0.0.0:8080")
}
4
  • Kindly post your code so that we can understand where the problem is. Ideally local host should hit. Commented Aug 16, 2023 at 3:14
  • did this helps you to understood the difference between localhost and 0.0.0. stackoverflow.com/questions/20778771/… and stackoverflow.com/questions/59179831/… Commented Aug 16, 2023 at 3:29
  • code posted as requested. But i think the answer below makes sense if it is correct :) Commented Aug 16, 2023 at 6:35
  • 1
    localhost is defined as 127.0.0.1 which is the container-private loopback interface; if you only bind to 127.0.0.1 then the process won't be reachable from outside its container. Listening to 0.0.0.0 means "all interfaces" which is pretty much required in Docker.
    – David Maze
    Commented Aug 16, 2023 at 11:08

1 Answer 1

0

In the context of networking, there is a significant difference between localhost and 0.0.0.0, especially when dealing with Docker containers.

  • localhost: When you use localhost as the host in your code (e.g., router.Run("localhost:8080")), the server will only listen for incoming connections on the loopback interface of the container. This means that the server will only be accessible from within the container itself, but not from the host machine or any external devices.

  • 0.0.0.0: When you use 0.0.0.0 as the host (e.g., router.Run("0.0.0.0:8080")), the server will listen for incoming connections on all available network interfaces, including the loopback interface as well as any external network interfaces. This makes the server accessible not only from within the container but also from the host machine and external devices.

In the context of a Docker container, using localhost only makes the service available within the container itself. If you want to expose the service to the host machine or other devices, you need to use 0.0.0.0.

When you run a Docker container with the -p option (e.g., docker run -p 8080:8080 <IMAGE_NAME>), you are binding a port on the host machine to a port in the container. In this case, the syntax -p HOST_PORT:CONTAINER_PORT indicates that any incoming traffic to the host's HOST_PORT will be forwarded to the container's CONTAINER_PORT.

So, if you use router.Run("0.0.0.0:8080"), the server in the container listens on all interfaces and accepts incoming connections from the host's 8080 port, which is then forwarded to the container's 8080 port.

In summary, the key difference is that 0.0.0.0 allows you to expose your service beyond the container's boundary, making it accessible from the host machine and other devices, while localhost confines the service to the container itself.

2
  • 1
    sounds very chatgpt haha but understood thanks Commented Aug 16, 2023 at 6:33
  • So what's the way out? Can it be used?
    – membrandt
    Commented Aug 16, 2023 at 8:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.