Simple cross container communication?

I’m very new to docker and simply want to use redis from my own project in a different container.

Virtually all guides describe linking containers but the official docs say that that is deprecated and one should use bridge networks instead.
So I assigned a hostname to my redis container and hoped that I would be able to reach it that way (hardcoding the IP is not an acceptable option to me) - no dice:

docker run --rm -p=6379:6379 --hostname=redis redis:3-alpine

In a terminal inside of the container of my project:

/home/version-frontend # ping redis
ping: bad address ‘redis’
/home/version-frontend # ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.267 ms

Anything that I’m missing? Surely, there must be a way to reference other containers from within another on the same host aside from assigning a static IP? (Links to docs would be appreciated)

Ah… The sweet smell of DNS issues :slight_smile:

The problem seems to be that your container ignores that the redis name should resolve to 172.17.0.2 ip address. That is the job of your hostfile or of a DNS server

Take your pick:

  • Either you edit your hosts file (/etc/hosts) and add the redis -> 172.17.0.2 mapping on all machines you require name resolution

or

  • Use dnsmasq or a proper DNS service like BIND and point your DHCP DNS entry to the dnsmasq/BIND server.

Ample information is available online on how to achieve any of these. Now you know what to look for, remember Google is your friend!

Good luck

Thanks but… regardless of a working solution that now makes me wonder… docker assigns an IP to each container. It has the knowledge of that correlation. And apparently, it used to be possible to make use of that knowledge by linking containers. Which makes a lot of sense to me.

But now, the intended workflow is that I either define the mapping to a specific IP (which I have to either find out or define in advance) myself or use an additional service?

Do I understand this correctly?

1 Like

I am no expert so others may chime in with more accurate answers but it is my understanding that if you do not wish to use the legacy --link and use the default bridge networking, you will not be able to take advantage of Docker’s automatic name resolution and will need to take care of it yourself.

If you do want to use Docker’s automatic name resolution, you will need to make your own user-defined network as demonstrated in this fine piece of documentation.

User-defined networks
It is recommended to use user-defined bridge networks to control which containers can communicate with each other, and also to enable automatic DNS resolution of container names to IP addresses.

Hope this helps

1 Like

Woohoo! So a solution adequately simple for this task WAS/IS possible! Thanks a lot!

As described in that fine piece of documentation (the thing is - while it describes things well, it is more elaborate than what I thought would be appropriate for my use case, so it could be that I already skimmed it and didn’t realize that it actually is what I need), I simply created a my own bridge network, assigned a name to both my containers, joined the network with them and done.
For anybody else stumbling across this - this simply means:

docker network create -d bridge mybridge
docker run --rm --network=mybridge -p=6379:6379 --name=redis redis:3-alpine
docker run --rm --network=mybridge -it -p=443:443 --name=version-frontend version-frontend

And boom…

/home/version-frontend # ping redis
PING redis (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.154 ms

This is fantastic! Gotta say though - I wish there were simple examples like this one around. Because while it is certainly important to have the docs available that explain how to setup more complex scenarios or explain things in detail, I think it would also help to demonstrate simple workflows suitable for small projects and/or beginners.

4 Likes