I deleted my previous post and reverted the question to the previous version as well. I misunderstood the situation based on posts in the past although asking about the RAM was not really related to whether the container name can be set when using replicas.
So now to add something actually useful to the conversation eventually, I can say that setting the reason of not supporting the container name when also using replicas is that you would have multiple containers with the same name. And you already know that is not possible. Docker will generate names only whene there is no name specified and it wonât even add suffixes to replicas, although that would make sense, but that parameter should probably be called container_name_prefix or something like that.
Hello,
Thank you so much for your reply.
1- When should I use replicas? Is this for when a container needs high availability?
2- So the amount of resources used is equal to the number of containers multiplied by the amount of resources defined for the container. Right?
3- I removed the container_name and ran the containers, but I got the following error message:
# docker compose up -d
[+] Running 2/5
â Š Network hub_default Created 4.7s
â Container Node Started 2.5s
â Container hub-nginx-3 Created 3.9s
â Container hub-nginx-2 Starting 3.9s
â Container hub-nginx-1 Started 2.6s
Error response from daemon: driver failed programming external connectivity on endpoint hub-nginx-2 (e84ec06babdd0b15508adcda922fd4dd532048a1efdf7a925114d2997002783c): Bind for 0.0.0.0:80 failed: port is already allocated
It is normal that one port cannot be used multiple times, so shouldnât the port be specified with the replicas option?
Docker Swarm is used to orchestrate containers across multiple nodes, connected to a Swarm.
You need to tell it how to deploy the containers. How many you want, how to distribute them, if they should be constraint to certain nodes, or if every node should get a single instance (mode: global).
Usually you would not expose a port on a swarm service, as multiple containers might be deployed on the same node, so you would see that error.
Having multiple replicas/instances per se is not really providing any HA. Only internally if you use Docker DNS to access a target service.
Best practice is to deploy a reverse proxy on required nodes that manages internal forwarding to target services, that can be scaled higher than the number of nodes.
docker service create --name My-Nginx --replicas 3 -p 80:80 nginx
will work, it will use name as prefix (the behavior is a bit different to compose) and create a ârouting meshâ (doc) on port 80 and forwards requests to nginx instances.