0

I am running Docker version 19.03.4 on Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-140-generic x86_64). Inside I start two images:

Image 1

I expose the following ports: 0.0.0.0:3333->3000/tcp (that's Grafana).

Image 2

I expose the following ports: 0.0.0.0:8812->8812/tcp, 0.0.0.0:9000->9000/tcp, 0.0.0.0:9009->9009/tcp (that's QuestDB).

The I go to localhost:3333 (Grafana) and try to connect to QuestDB (localhost:8812), but it does not work.

When I go back to my machine's terminal and type telnet localhost 8812 everything works just fine.

Can you help me find the reason why through one docker image I can't seem to find the port of another?

FWIW: I also tried creating a docker network, where I added both images to that network, but that did not work either.

Newskooler
  • 3,973
  • 7
  • 46
  • 84
  • Please read: [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – Turing85 May 16 '21 at 10:49
  • The linked question goes into the standard recipes using `docker network create` and Compose (and also has a very detailed, but outdated, answer using the obsolete links feature). `localhost` almost always means "this container", not the host or another container. [Networking in Compose](https://docs.docker.com/compose/networking/) in the Docker documentation is also useful background reading even if you're not using Compose. – David Maze May 16 '21 at 11:08

1 Answers1

2

When you create a user-defined bridge network and connect each container to it, they can be addressed using their container name thanks to a service called automatic service discovery.

So if you have two containers called grafana and questdb, from inside grafana, you can access questdb using its name:

# From grafana
ping -c 2 questdb

Additionally, this topic is discussed in this thread to a great extent.

boranseckin
  • 335
  • 2
  • 12
  • okay, but how can I define this in localhost terms? because in Grafana, I need to specify the `Host` in order to make a connection; it's just that when I write `localhost:8812` via the Grafana UI, it does not connect. – Newskooler May 16 '21 at 09:56
  • When you use `localhost` inside the container, it does not target your local machine. Instead, it targets the container you are in. If you want `localhost` to point to your local machine you can use `--network=host` ([Docker docs](https://docs.docker.com/network/host/)). Also, see this [answer](https://stackoverflow.com/a/24326540/10161292) from Thomasleveil for more information. – boranseckin May 16 '21 at 10:13
  • I would recommend using a custom network as explained above. You can still access your local machine using the `gateway` address of that network. To find it, you can use `docker network inspect [network-name]` command. ([Docs](https://docs.docker.com/engine/reference/commandline/network_inspect/) for network inspect) – boranseckin May 16 '21 at 10:25