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...
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).
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.1corresponds to your host's loopback adaptor0.0.0.0is effectively any network adaptor on this host (including127.0.0.1)localhostis the DNS name that is customarily mapped to127.0.0.1
So the consequence should be the same regardless of which of these approaches you use.
HTH!