How to bind the listening ports to localhost when using network_mode: host?

So I have some weird requirements:

One pgsql server on the localhost.

One dockerized grafana to connect to that server. And said grafana exposed on 3000.

Plus an nginx that provides sll for grafana.

I use netwrok_mode = host so grafana can connect to pgsql without worrying about tweking the access in pg_hba.

It will connect to localhost:5432.

And everything works ok just the port exposed by grafana is *:3000

Is there a way to expose that port as localhost:3000 instead of *:3000 in docker compose?

Thanks.

I changed the apps in the containers to listen on localhost and that did.it

Still I would like to know if that binding is possible through docker mechanisms.

Since network_mode: host results in a lack of network namespace isolation, the container effectively uses the network devices from the host, and behaves network-wise like any native process on the host.

The solution is indeed to configure the application inside the container to bind the ip you need, the same way you would need to configure it for a native process on the host itself.

1 Like