21

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?

Max Romanovsky
  • 2,824
  • 5
  • 30
  • 37

3 Answers3

9

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.

6

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

Xiongbing Jin
  • 11,779
  • 3
  • 47
  • 41
  • 1
    How would that work with a tool like nginx where the config is predefined? Would I have to use Docker events to scan for new hosts and update the list of backends? (similar to how interlock works) – tpbowden Mar 16 '16 at 15:04
  • I've tried to use container names, but it seems that they are not populated to DNS – Max Romanovsky Mar 16 '16 at 15:53
  • @tpbowden One way would be to edit the nginx config and restart nginx after adding additional containers. Auto-scaling of the load balancer is possible with projects like https://github.com/docker/dockercloud-haproxy, but it seems like these work by bind-mounting the docker daemon socket into the container. – bonh Feb 01 '17 at 04:27
2

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)

Community
  • 1
  • 1
luckydonald
  • 5,976
  • 4
  • 38
  • 58