How do I scale two services in docker-compose (one being a network) and keep them connected to each other's network_mode?

Viewed 102

In a scenario where one service connects to the other one's network (as in the example .yml below), is there a way to use --scale to scale both and make them connect to the correct network?

As in app1 uses vpn1 network, app2 uses vpn2 network etc.

services:
    vpn:
        image: myvpn

    app:
        image: myapp
        depends_on: vpn
        network_mode: service:vpn

I want to be able to run the container with docker-compose up -d --scale app=5 --scale vpn=5

The issue is that if I scale both containers the way it's currently setup (lets say with two instances of each service to simplify), both "app" services connect to the first "vpn" service.

I can confirm it by inspecting both app-1 and app-2. In "HostConfig" they both show "NetworkMode": "container:ef37426bec3dbd9c182187d87faf5fe8c92c1e1fa26066f57d163f301af2574e", which is the first vpn container.

I understand this is the expected behavior, as the .yml file indicates network_mode: service:vpn and not something like network_mode: service:vpn-${container ID here}

I want to find a way to set app-1 to use vpn-1 as NetworkMode, app-2 to use vpn-2 as NetworkMode, etc.

1 Answers

Out of the box docker-compose allows for two communication methods:

  • VIP - one VIP and DNS record to load balance between the replicas using docker's configuration
  • DNSRR - one DNS record and multiple IPs (one for each service) used to allow the user to implement their own load balancer

DNS lookup result

As far as I managed to research, a configuration as the one you're asking can be achieved using docker-compose in one way only, you can set in each client service the backend DNS name by the container name.

For example, if I want to connect to the second instance I'll connect to docker_backend_2

container names

Another way that requires a more hands-on approach would be talking between the services and deciding which will connect to who of the returned IP's to the nslookup or some other probing method

If this connectivity configuration is important to you, you should look into Kubernetes where your basic working unit is a pod and you can spin multiple container images per pod, then you'll spin one of each service inside the pod and connect directly to them internally in the pod's network.

Related