This post is part of the series Dev with Docker
Other posts in this series:
Docker Engine has some built-in networking features 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
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 Xdebug. Using Docker for Mac, there’s not a reliable address you can use to make that connection. A container that tries to connect to
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. 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).
Verify that it worked with:
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 the launchd plist file floating around, 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.
Continue reading this series:
nginx as HTTPS proxy for Elasticsearch