Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do links and scaling work together in docker compose?

Here's my understanding of docker compose:

  • You can "scale" a service to run it in multiple containers at once.
  • If you link service A to service B, service A has access to a container running service B.

Is my understanding correct, and if so, where does a link connect if there are multiple containers running the service?

like image 407
Gaelan Avatar asked Apr 19 '15 04:04

Gaelan


People also ask

What does links mean in Docker compose?

According to the Docker Compose's compose-file documentation: depends_on - Express dependency between services. links - Link to containers in another service and also express dependency between services in the same way as depends_on.

How do you scale a docker compose?

This can be controlled by assigning port range on the ports section of compose yaml file. Scaling can also be done by using up command as well with the --scale flag. Alternatively, in Compose file version 3. x, you can also specify replicas under the deploy section as part of a service configuration for Swarm mode.

Is Docker compose links deprecated?

Warning: The --link flag is a deprecated legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link.

How does link work in Docker?

Docker also has a linking system that allows you to link multiple containers together and send connection information from one to another. When containers are linked, information about a source container can be sent to a recipient container.


1 Answers

First of all I would clarify that, by default, with or without linking a container with other, all container has visibility to other containers running in the same host (using the container IP). You can change this behavior using the icc=true flag in docker daemon.

In respect of the links with docker-compose, these are generated when the container with the links are created. Let's see it with an example. Using this docker-compose.yml

web:   build: .   command: python app.py   ports:    - "5000:5000"   volumes:    - .:/code   links:    - redis redis:   image: redis 

After running docker-compose up -d the web_1 container is linked with the container with redis_1:

        (...)         "Links": [         "/compose_redis_1:/compose_web_1/compose_redis_1",         "/compose_redis_1:/compose_web_1/redis",         "/compose_redis_1:/compose_web_1/redis_1"     ], (...) 

Now we want to scale the redis service using docker-compose scale redis=2. After running it (and create a new container redis_2), the links in web_1 keeps unchanged.

        (...)         "Links": [         "/compose_redis_1:/compose_web_1/compose_redis_1",         "/compose_redis_1:/compose_web_1/redis",         "/compose_redis_1:/compose_web_1/redis_1"     ], (...) 

It is needed to stop, remove and run web_1 to see these links created:

docker-compose stop web docker-compose rm web docker-compose run -d web docker inspect compose_web_run_2 (...)        "Links": [         "/compose_redis_1:/compose_web_run_2/compose_redis_1",         "/compose_redis_2:/compose_web_run_2/compose_redis_2",         "/compose_redis_1:/compose_web_run_2/redis",         "/compose_redis_1:/compose_web_run_2/redis_1",         "/compose_redis_2:/compose_web_run_2/redis_2"     ],(...) 

And the /etc/hosts of web_1 container:

172.17.0.24 7be2dabea910 127.0.0.1   localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.21 compose_redis_1 8a1297a5b3e4 172.17.0.23 compose_redis_2 069dd46836aa 172.17.0.21 redis 8a1297a5b3e4 compose_redis_1 172.17.0.21 redis_1 8a1297a5b3e4 compose_redis_1 172.17.0.23 redis_2 069dd46836aa compose_redis_2 

So to generate the new links, you is is needed to stop, remove, and run again the container.

like image 104
Javier Cortejoso Avatar answered Oct 01 '22 08:10

Javier Cortejoso