localhost always (every platform, every Docker setup) refers to the container itself and never to anything running in any other container or the host system. (Unless you’re running a container with --net host
, which is odd on Docker for Mac.)
On every platform with every Docker setup, if you can identify a “real” IP address for the host system, you can use it to connect to services that are running there, including other Docker containers that publish ports with docker run -p
. Some platforms/setups provide a bridge address that you can use to access the host system (frequently 172.17.0.1/16 on native Linux and 192.168.99.100/24 on Docker Toolbox/Docker Machine) but Docker for Mac does not have this.
localhost always refers to the current container. You have to use the compose name.
Since you declare that as a published port with a Docker Compose ports
section, yes.