After a lot of research, I found two supported ways of doing this:
- Run your service in another container on the same host. Then you can connect the two using
--link
. More info here.
- If, as in my case, you want to emulate having the service run outside of a Docker container (because this is how you have things running in prod, or because moving the service into a docker container is too much trouble, or any other reason), you can do this by passing information about your host to Docker, so that it thinks of it as any other computer. If you're using boot2docker, you're host is always mapped to 192.168.59.3, so all I do is create an alias to that and tell Docker about it at run time with the
--add-host
flag. Example: docker run --add-host=localbox:192.168.59.3 ...
. This let's me now hit the alias "localbox" to access anything on my localhost from within Docker. To connect to mysql on the localhost from within Docker, for example, I'd just do mysql -h localbox
.
Finally, a caveat for #2: your service will treat stuff trying to connect from within Docker to your service on localhost as a foreign process, so for this to work, you'll need to make sure the service is listening not just on 127.0.0.1, and depending on what you add, you'll need to open up your firewall to allow connections as fits your needs.