2

There are a few similar questions on SO like this one, but as far as I can tell, the Docker ecosystem has moved a lot since it was asked, and I still can't find a canonical answer anywhere.

So, if I have a database like Redis running on my host, and I want my container to have access to it, what's the canonical way to do that?

Community
  • 1
  • 1
Eli
  • 36,793
  • 40
  • 144
  • 207

2 Answers2

1

Unless you have something like a firewall blocking it (as the question you refer to did), you just connect to it as if you were on another machine.

Your container will have its own IP address and virtual Ethernet device; Docker will have set up a route from that device over the Docker bridge to the host; it should all work fine.

I could think of a dozen other reasons for you to have trouble, but will leave you to mention what specific problems you are encountering.

Bryan
  • 11,398
  • 3
  • 53
  • 78
0

After a lot of research, I found two supported ways of doing this:

  1. Run your service in another container on the same host. Then you can connect the two using --link. More info here.
  2. 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.

Eli
  • 36,793
  • 40
  • 144
  • 207