Why does it throw an error, when I set container_name on a replicated service?

The YAML file is as follows:

      image: nginx:latest
      container_name: Nginx-NodeJS
        mode: replicated
        replicas: 3
            memory: 100M
            cpus: '0.50'
            memory: 64M
        - '80:80'

I have some questions:
1- When should I use the replicas option? Is this some sort of HA for containers?

2- Does each container take up as much CPU and RAM as I specified? For example, replicas: 2 means 512MB of RAM and 256MB reserved RAM.

3- Why do I get the following error message when I try to run the container?

services.deploy.replicas: can't set container_name and nginx as container name must be unique: invalid compose project

Thank you.

You can’t have multiple containers with the same name, they have to be unique.

replicas: x will create multiple containers.

So you can not set a fixed container_name, it will be set automatically by compose.

1 Like

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.

1 Like

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 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.

It uses a Docker network, no ports need to be exposed by the target services. See simple Traefik Swarm example.

1 Like

Thanks again.
I want to use the article Docker Swarm Basics: A Step-by-Step Guide for Beginners | by Nidhin B | Medium. Does worker node mean containers?

A node is a server, with Docker Swarm it can be manager or worker.

You should have 3 managers for HA, in case one falls over, to still be able to “orchestrate” services/containers.

But you can run workloads on both.

1 Like

Thanks again.
What is TOKEN?

# docker swarm join - token <TOKEN> <MANAGER_NODE_IP>:2377

The article used the following command:

# docker service create - name My-Nginx - replicas 3 -p 80:80 nginx

Does the above command create three Nginx containers that use port 80 at the same time?

It seems you need a private instructor for all the questions you have :smile:

Please start by:

  1. reading the docs (link)
  2. reading some tutorials (older link)
  3. watching some tutorial videos

The command

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.

1 Like

Thanks again.
Not really. How can multiple containers use a port at the same time?