docker 容器主机名序号

docker container hostname sequential number

在 docker 群中 replicas/scale 的情况下,是否有任何选项可以让主机名/容器名具有一些序列号?

version: '3'

services:

  serA:
    image: someimage1
    hostname: serA
  serB:
    image: someimage2
    tty: true
    hostname: serB{{.Node.index}}
    deploy:
      replicas: 2

例如:我希望主机名为 serB1serB2

据我所知,这不受支持,也不会得到支持,因为 Docker 团队认为这是个坏主意:

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

https://github.com/docker/compose/pull/1131 and https://github.com/docker/compose/pull/1371