This post is part of the series Other posts in this series:. Reaching localhost from a Docker container (Current) Docker Engine has some to allow containers to communicate with each other and with the outside world.
For example you can link two containers together to allow them to talk to each other, either via the docker run command or in your docker-compose.yml. Version: '2' services: memcached: image: memcached:1.4-alpine php: image: php:5.6-fpm links: - memcached nginx: image: nginx:stable-alpine links: - php ports: - '80' In our example, the php container can communicate with the memcached container, the nginx container can communicate with the php container, and the nginx container exposes its port 80 to receive connections from, for example, your web browser. In some cases, though, your container needs to be able to reach back out to your host system. The specific use case I have is debugging with.
Using Docker for Mac, there’s you can use to make that connection. A container that tries to connect to localhost or 127.0.0.1 will be talking to itself, not to your host machine. To work around this, I set up an additional IP address for my host OS’s loopback interface. I use 10.254.254.254, but you may find it conflicts with other applications you have running, so any other local address will be effective. The command to set up this address is: sudo ifconfig lo0 alias 10.254.254.254 Running this will allow any container you have running to connect back to your host OS using the address 10.254.254.254. My passport for mac on windows 10. You can, for example, set this in your php.ini as the remote host address for Xdebug (I prefer to set it in a reverse proxy configuration, but that’s a topic for a later post).
Xdebug.remotehost=10.254.254.254 Verify that it worked with: ifconfig lo0 Your new address should appear there along with a few other defaults. You’ll have to run the command after every boot, unless you set a launchd script to do it for you. There are a few versions of, or you can make your own from one of them. Copy it into /Library/LaunchDaemons/ and your new loopback address will be set for you on every boot.
This launchd script will ensure that your Docker environment on your Mac will have 10.254.254.254 as an alias on your loopback device (127.0.0.1).