0

I can ping InfluxDB running inside a docker container, with a port exposed in the host, from the host:

» curl -k -L -I https://localhost:8086/ping
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 2bb1059b-360e-11e7-8001-000000000000
X-Influxdb-Version: 1.2.0
Date: Thu, 11 May 2017 05:53:34 GMT

I run an Ubuntu 16.04 docker container (with curl installed), connected to the same network as the InfluxDB container, and I was not able to ping localhost:8086. Finally I found out that I need to ping using the IP address of the InfluxDB container:

root@4a5457a5e297:/# curl -k -sL -I https://172.18.0.1:8086/ping
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: d8ab4282-360e-11e7-8002-000000000000
X-Influxdb-Version: 1.2.0
Date: Thu, 11 May 2017 05:58:25 GMT

Which means that first I need to find out the IP address of the InfluxDB container. I just guessed, since I was not able to do ifconfig in the InfluxDB container, and I have no idea how to list the IPs of all running containers: docker ps does not show it.

So, the port of InfluxDB is exposed in the the host as localhost:8086, but it is not exposed to the other containers. Some questions:

  1. Is it possible to address containers by name? Does docker automatically assign DNS entries to the containers, and can those be resolved from withing the containers? From within the host? What is the naming scheme?
  2. Is it possible to expose ports from one container not only to the host, but to all other containers (running in the same network), so that I can ping localhost:8086 from any container?
  3. How do I get a list of IPs for all running containers?
blueFast
  • 41,341
  • 63
  • 198
  • 344

2 Answers2

2

I run an Ubuntu 16.04 docker container (with curl installed), connected to the same network as the InfluxDB container, and I was not able to ping localhost:8086. Finally I found out that I need to ping using the IP address of the InfluxDB container:

When "speaking" to other containers on the same network, please use the container name. You can enforce this with --name influxdb

Then you'll be able to, from the same network, use curl http://influxdb:8086

Rawkode
  • 21,990
  • 5
  • 38
  • 45
  • Yes, I remembered about this and it works. Still, it would be good to be able to expose ports to other containers, so that related services can be found in `localhost:port` – blueFast May 11 '17 at 08:58
0

1. Container communication

As Rawkode mentioned, it is possible. But for some reason I can not do that with the default bridge network.

$ docker run -itd --rm --name test1 alpine /bin/sh
726cd933446df40e78e760d86256e11b1e786d83057a9d075c05c4d38240656c
$ docker run -itd --rm --name test2 alpine /bin/sh
c6837529c37f486edbc3a7743a6b127b9bdaae8a619564368697137fd8ae5622
$ docker container exec test1 ping test2
ping: bad address 'test2'
$ docker container exec test2 ping test1
ping: bad address 'test1'
$ docker container stop test1
test1
$ docker container stop test2
test2

Creating a test network will work:

$ docker network create testnet 
ca1db96c3a533033c68d8885fac2f354919edc810e0f376f06f86e45d3050b35
$ docker run -itd --rm --name test1 --network testnet alpine /bin/sh
187f2c8534504e6a8db96c0a731c735976b19b0a710e162f3537b2f5f16d7b05
$ docker run -itd --rm --name test2 --network testnet alpine /bin/sh
7efd386c13962a56dc074903373848174fb5cdb649038a6e67fbb2f0f8bde74b
$ docker container exec test1 ping test2
PING test2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.076 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.165 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.159 ms
64 bytes from 172.18.0.3: seq=3 ttl=64 time=0.077 ms
^C
$ docker container exec test2 ping test1
PING test1 (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.063 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.235 ms
64 bytes from 172.18.0.2: seq=2 ttl=64 time=0.218 ms
64 bytes from 172.18.0.2: seq=3 ttl=64 time=0.099 ms
^C
$ 

2.

Containers can communicate directly, you do not have to use localhost. Take a look on how to expose ports

Basically, you have three options:

  • Neither specify EXPOSE nor -p.
  • Only specify EXPOSE.
  • Specify EXPOSE and -p.

If you do not specify any of those, the service in the container will not be accessible from anywhere except from inside the container itself.

If you EXPOSE a port, the service in the container is not accessible from outside Docker, but from inside other Docker containers. So this is good for inter-container communication.

If you EXPOSE and -p a port, the service in the container is accessible from anywhere, even outside Docker.

found here: https://stackoverflow.com/a/22150099/1561148

3. inspect network

$ docker network inspect testnet | grep IPv4
            "IPv4Address": "172.18.0.2/16",
            "IPv4Address": "172.18.0.3/16",
Community
  • 1
  • 1
tgogos
  • 23,218
  • 20
  • 96
  • 128