0

Hello is it possible to access localhost in Docker and test web app on mobile device? Computer and smartphone are in the same WiFi. I would be pleased for any advice. :)

Kay
  • 591
  • 1
  • 7
  • 27
  • Hi @Kay. How do you run your docker container? Can you please describe your issue in more detail? – Hayk Davtyan Nov 06 '21 at 20:31
  • The container itself, the computer hosting the container, and the smartphone client each believe they are `localhost`; one of those can't use `localhost` to refer to another. Also see [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) (though this won't help the remote client) or [What does localhost means inside a Docker container?](https://stackoverflow.com/questions/50278632/what-does-localhost-means-inside-a-docker-container) – David Maze Nov 06 '21 at 21:37

1 Answers1

0

Yes, it is possible but it's not OS independency so it's good for a dev/local environment only.

Besides, it's quite vary because Docker will try to create a virtual network/adaptor on your docker machine and each different OS has their owned way and limitations.

In Window you can use docker.for.win.localhost to access host network from inside container. For MacOS, there is docker.for.mac.localhost. Docker >= 18.03 will support host.docker.internal but there is no gurantee that it will work on all platforms, so you will have to try it.

There is another safer solution and OS independent, which is using container network in host mode https://docs.docker.com/network/host/. In this mode, your container will use host machine network directly and every port exposed inside container will be exposed outside too. And of course you can access others services running on host machine by just using localhost.

I recommend you to use the docker built-in docker.for.xxx.domain if you are in dev environment and still keep the network isolation, so any security problem inside container won't affect your server. Otherwise, host mode network is a wider compatibility choice.

Truong Hua
  • 802
  • 6
  • 18