Cannot access Spring boot app from Docker host and port after deploying to Docker Containers. Solved!

I recently faced an issue where I deployed our spring boot application Docker environment. Everything worked our perfectly fine like app was getting started in docker env. as per logs.

But when we tried to access the app endpoints from the docker host and port or the load balancer it was the container was unreachable.

We tried to debug the issue by accessing the app inside container using localhost and it surprised us that application endpoints were reachable from inside the container.

Now the issue was that the endpoints were not reachable from container.

The issue could be the pot mapping issue but it was not.

The app was listening to localhost server with address 127.0.0.1

In our Spring boot application.properties file when we added below properties, it fixed the issue.

server.address = 0.0.0.0

So what is the difference between 0.0.0.0 and 127.0.0.0?

  • 127.0.0.1 is the loopback address (also known as localhost).
  • 0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown or non applicable target (a no particular address placeholder).

In the context of servers, 0.0.0.0 means “all IPv4 addresses on the local machine”. If a host has two ip addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs.

Which means that now our app when started at 0.0.0.0 listens to all container IPv4 now.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s