Is there any option to have the hostname / containername to have some seq numbers in case of replicas/scale in docker swarm?
version: '3'
services:
serA:
image: someimage1
hostname: serA
serB:
image: someimage2
tty: true
hostname: serB{{.Node.index}}
deploy:
replicas: 2
For ex: I would like to have the hostnames as serB1
and serB2
In the same way, a container's hostname defaults to be the container's ID in Docker. You can override the hostname using --hostname . When connecting to an existing network using docker network connect , you can use the --alias flag to specify an additional network alias for the container on that network.
To help the humans, Docker also supplies containers with a randomly-generated name from two words, joined by an underscore, e.g. evil_ptolemy . This can make it easier to tell one container from another, but the random names don't give any more insight into the container function than the UUID.
The typical organization that uses a container orchestrator runs 11.5 containers per host, as compared to about 6.5 containers per host in unorchestrated environments.
AFAIK this is not supported and won't be supported as the Docker team thinks it's a bad idea:
After some thought, I don't think this is a good idea.
We might well get rid of scale numbers in future. Once we move to multi-host systems with Swarm, keeping track of an incrementing sequence of integers will become (a) costly and (b) unreliable.
Even if we kept scale numbers, I'm not convinced it's a good idea to rely on them for dividing up work between containers. Better for the container to generate an identifier of its own and use that, so it's not dependent on Compose to work properly.
...
To recap some of the discussion:
$(hostname) or $(hostname -i) can be used to get a unique id, or the ip address of a scaled container. Using this does not duplicate images, it's evaluated at container run time.
we'd like to remove the sequential scale ids and only use the uniqueness of the container ids. They are not reliable in a distributed system
see https://github.com/docker/compose/pull/1131 and https://github.com/docker/compose/pull/1371
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With