问题
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
回答1:
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
来源:https://stackoverflow.com/questions/43659410/docker-container-hostname-sequential-number