3

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

suresh
  • 254
  • 5
  • 9
  • What is the problem with `-p` option. Does `localhost:4200` on your host is not pointing to the container 4200 port? – mchawre Jul 15 '19 at 17:14
  • Hm... the question is quite confusing. Can you tell us more about the problem you are facing? – mixth Jul 15 '19 at 17:21
  • Possibly duplicate with this question? https://stackoverflow.com/questions/30109037/how-can-i-forward-localhost-port-on-my-container-to-localhost-on-my-host – Haoming Zhang Jul 15 '19 at 17:54

1 Answers1

3

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!

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • `--publish=127.0.0.1:4200:4200/tcp` --> this is really saying that 4200 on 127.0.0.1 is mapped to 4200 in the container. How should I map localhost:4200 in container to 127.0.0.1 on host? i.e. i want something like `--publish=4200:localhost:4200/tcp`, but this is throwing Invalid ip address error on docker run. – suresh Jul 16 '19 at 19:17
  • Please ensure you read the documentation. This is explained there. The example I provided maps the container's port `:4200` to the host's port `:4200`. If you wish to map to a different port on the host, you may use `XXXX:4200` where `XXXX` is the host port that you wish to use. You cannot arbitrarily change the container port because then the container's binary will not be published. – DazWilkin Jul 16 '19 at 20:02
  • `docker run ...` when used in this way, insulates you from the complexities of the container networking. You may assume that, when provided `--publish=XXXX:YYYY` that Docker is able to bind your host's port to the container's port. You may omit or include the host's IP address in the `--publish` for the reasons I provided. – DazWilkin Jul 16 '19 at 20:05
  • The issue I am facing is, if the container is exposing the app on localhost:4200 (stressing on localhost again), I am not able to access from the host with above port binding. If the container is serving the app on 0.0.0.0:4200 then I can access from the host. – suresh Jul 16 '19 at 21:01
  • 1
    I think you cannot. If the process *inside* your container is bound to localhost, localhost uses a special loopback interface that is distinct from regular networking. You will be able to access the service from within the container (host) but you won't be able to route its traffic externally. – DazWilkin Jul 16 '19 at 21:48