suresh
suresh

Reputation: 284

How to expose app running on localhost inside docker?

I have an app running inside a docker container on localhost:4200 (not 0.0.0.0:4200). How can I expose it to the host?

When I use -p 4200:4200 on docker run, I am not able to get to the app. i.e. curl localhost:4200 or curl http://127.0.0.1:4200 or curl http://<ip from ifconfig>:4200 or curl http://0.0.0.0:4200 doesn't work. However, docker exec <container-id> curl localhost:4200 works. This means that app started successfully but 4200 port on localhost from container is not exposed to the host.

I stopped the app and modified the app (on the same running container) to expose the app on 0.0.0.0:4200 instead of localhost:4200. After that, I am able to get to curl http://0.0.0.0:4200.

I am trying to figure out on how can I expose the port on localhost from container to host (so that I don't have to modify my app).

Upvotes: 3

Views: 3933

Answers (1)

DazWilkin
DazWilkin

Reputation: 40061

You can be more explicit and use:

docker run --publish=127.0.0.1:4200:4200/tcp ....

See documentation

However:

  • 127.0.0.1 corresponds to your host's loopback adaptor
  • 0.0.0.0 is effectively any network adaptor on this host (including 127.0.0.1)
  • localhost is the DNS name that is customarily mapped to 127.0.0.1

So the consequence should be the same regardless of which of these approaches you use.

HTH!

Upvotes: 4

Related Questions