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 or curl from ifconfig>:4200 or curl 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 .

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).

3

1 Answer

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!

5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest