0

How can I have two or more containers write data out to localhost on the same port? Is the only way to do this run the destation service in a container as well and have the write clients write to the container's IP/service name?

Specifically, I have 3 containers that are writing data out. I want them to write a local database running on localhost:8086.

Sam Dillard
  • 670
  • 1
  • 5
  • 18
  • What platform are you running on? Take a look at https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container/31328031#31328031 for some possible solutions. – larsks Feb 05 '21 at 00:31
  • I'm on Mac. Does it require a hack or is it simple and I'm just missing something? – Sam Dillard Feb 05 '21 at 00:36
  • If you have MySQL running on your Mac (and listening on all interfaces), you can access it at the special hostname `host.docker.internal`. If that's what you were asking about. I'm not entirely sure I'm reading your question correctly. – larsks Feb 05 '21 at 00:42
  • I'm not sure how it would work on Mac, but on Linux you can access your host through 172.17.0.1 (its IP in the default Docker network). Not suitable for production, of course, but very convenient for testing. – Leonardo Dagnino Feb 05 '21 at 02:47

1 Answers1

0

Do you mean your target database to write with is running on your Host machine? and your clients that would write runs inside the container? If so, you can publish a port from your Host machine to your containers then inside your container, you can access your Host machine's port.

$ docker run --name my-database-client-1 -p 8086:8086 ...

Or, if your database also runs inside the container, it's better to use a network then reference your server-database using its container name i.e. my-database-server:8086.

sareno
  • 576
  • 8
  • 10
  • 1
    Publishing is for exposing your container's port through your host's interfaces - this would fail if you had your database running on the host, since Docker would try to bind that port (which is already bound) – Leonardo Dagnino Feb 05 '21 at 02:44