It is useless for fixing the ip of container for Swarm after enabling the replicas

hi all,

So far, I wanna apply swarm replicas to resolve the loading balance issue.

According to my requirements, I have to send the request from A container to B container that I did set the ipv4_address for the container, respectively. If I did not turn on the replicas, the IP can work. (Will not change)

However, if I turn on the replicas, the swarm will help me to create several containers (e.g., for setting 10 replicas, there are 10 containers.), which IPs are randomly defined. I check the docker network that there is no IP of what I define in the docker-compose file.

So I am wondering is it possible to define the IP for my main container? OR let the replicas IP number start from my defined IP in the docker-compose file?

Thank you in advance!

Best regards,

The ipv4_address field may be valid to be used in compose files, but still is ignored for swarm stack deployments. More precisly: swarm services do not support it (see: [epic] add more options to service create / service update).

Instead of trying to pin down container ip’s you might want to use the service name (which will resolve to the vip of the loadbalancer in front of the replicas) or tasks.service_name (which will resolve to the multi-value dns entries of the container replicas). This is the intended and desired solution for container to container communication.

I must miss something. Do you mind elaborting why you think why loadbalancing would need fixed ips?

Hi @meyay,

Thank you for your reply and your information!!

Indeed, my main target is to get the IP of the B container to let the A container be able to send the stuff via https to B container. Besides, I also have another purpose that my other container (out of this docker-compose file) asks for sending the request via https to B container. Hence, that is why I am trying to fix one of B container IP.

I am interested in your sharing about using the service name or tasks.service_name to resolve this problem, but I am not sure how to do it in the HTTPS request.

I would be very grateful for any feedback you can give me on my plan.

Thank you!!

Best regards,

Let me draft an example:

    - target: 8080
      published: 8080
      protocol: tcp
   endpoint_mode: dnsrr # no vip

In this example service_a can use https://service_b/... to access the service_b tasks (~=container) by the services vip and https://tasks.service_b to access the service tasks (~=container) by round robin name resolution.

While service_a can use https://service_c/... to access the service_c tasks by round robin name resolution.

To access containers from service_b outside a container network, you simply publish/map the desired container port to the swarm ingress, which binds it to all swarm node host ports. Then use https://swarm-node:8080/... to access the containers by the vip.

I missed the scenario. You will need to create a docker network that actualy is attachable and attach your other container to the same network, which should make the rules from above apply to this container as well.

In order for this to work, the stack must be deployed using docker stack deploy.

1 Like

hi @meyay,

I really appreciate your detailed teaching!
I will try it right now.
Thank you!!

Best regards,