Tristan Claverie
1 min readSep 25, 2016

--

Well, there is in fact a simpler solution than creating a network: do nothing at all, docker-compose and docker are doing it for you.

Explanation

When running docker-compose, it creates a custom network xxx_default, where ‘xxx’ is the name of the folder.

And when running containers on a custom network, docker creates them with an internal DNS listening on 127.0.0.11. Each container in the network is aware of each other, and can communicate with one another using the name of the service from the compose file.

version: ‘2’services:
php-fpm:
image: yani/php-fpm
nginx:
image: yani/nginx

So, with this file the php-fpm service can ping nginx and nginx can ping php-fpm without problems.

If you want to access nginx through the web alias, there you can use linking to create an alias (in fact, linking in compose has no other use).

version: ‘2’services:
php-fpm:
image: yani/php-fpm
links:
- nginx:web
nginx:
image: yani/nginx

php-fpm now can contact nginx through both the web and nginx hostname.

If you want to alias both services, then your method was the right one: tell compose to create a custom network (other than the default one) and define aliases over it.

Links are deprecated, and now custom networks are the preferred way to communicate between containers.

Hope this was clear enough :)

--

--