From inside of a Docker container, how do I connect to the localhost of the machine?

DockerDocker Networking

Docker Problem Overview


So I have a Nginx running inside a docker container, I have a mysql running on the host system, I want to connect to the MySql from within my container. MySql is only binding to the localhost device.

Is there any way to connect to this MySql or any other program on localhost from within this docker container?

This question is different from "How to get the IP address of the docker host from inside a docker container" due to the fact that the IP address of the docker host could be the public IP or the private IP in the network which may or may not be reachable from within the docker container (I mean public IP if hosted at AWS or something). Even if you have the IP address of the docker host it does not mean you can connect to docker host from within the container given that IP address as your Docker network may be overlay, host, bridge, macvlan, none etc which restricts the reachability of that IP address.

Docker Solutions


Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhilView Question on Stackoverflow