Docker Community Forums

Share and learn in the Docker community.

Get container number as environment variable when using target_num_containers


(Antony Jones) #1

I want to get the current container’s instance number (the ‘1’ part of microservices-1, for example) when running multiple containers per service.

This is useful for instance when using something like shortid:

shortid.worker(integer)

If you are running multiple server processes then you should make sure every one has a unique worker id. Should be an integer between 0 and 16. If you do not do this there is very little chance of two servers generating the same id, but it is theatrically possible if both are generated in the exact same second and are generating the same number of ids that second and a half-dozen random numbers are all exactly the same.

I can see this number on the end of the environment variable “MICROSERVICES_1_ENV_DOCKERCLOUD_CONTAINER_HOSTNAME” = “microservices-1”

But I can’t see the id on it’s own. Is it available anywhere?