Max Romanovsky
Max Romanovsky

Reputation: 2894

How to reach additional containers by the hostname after docker-compose scale?

I'm using the docker-compose 1.6 with docker 1.10.1 and version 2 of docker-compose.yml.

As described in Networking in Compose, new network is created and all containers are assigned hostnames in it after docker-compose up.

How can I reach additional (i.e. scaled) containers via hostnames after docker-compose scale <component>=2?

Upvotes: 21

Views: 10082

Answers (4)

nicolas de loof
nicolas de loof

Reputation: 2633

Each container is registered on network with the service name, so you don't need to know the actual container name as service_x but can directly use service.

When a service is scaled to X replica, DNS query will return multiple results:

/ # nslookup test
Server:     127.0.0.11
Address:    127.0.0.11:53

Non-authoritative answer:

Non-authoritative answer:
Name:   test
Address: 172.19.0.4
Name:   test
Address: 172.19.0.2
Name:   test
Address: 172.19.0.5
Name:   test
Address: 172.19.0.3

Application code can then rely on this to select a replica to connect to, or you can setup a proxy to offer some load-balancing

Upvotes: 1

luckydonald
luckydonald

Reputation: 6886

I have answered that question based on python with the docker api.

You basically can use the docker api, to figure out what your project name and service name is.

Then you can filter all available containers for the same project+service to get other instances of yourself.

With the lookup of the numbers you can finally construct the hostnames.

See How to get infos about ourself in a compose cluster (backup at gist.github.com)

Upvotes: 2

Roman Navrotskii
Roman Navrotskii

Reputation: 91

When using "scale", internal docker dns service resolves only full names like projectname_db_1, projectname_db_2 and so on. Certainly, it would be better to provide in addition resolving service name + index without project name prefix.

Upvotes: 9

Xiongbing Jin
Xiongbing Jin

Reputation: 12107

Based on examples I've seen, assuming your container name is db, the scaled containers have the names of db_1, db_2, etc...

Upvotes: 6

Related Questions