Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Export service replica index to container via some environment variable or system file #9153

Closed
vlad-vinogradov opened this issue Feb 9, 2022 · 2 comments

Comments

@vlad-vinogradov
Copy link

Since docker-compose service can be scaled, it would be useful to export to each container the replica ordinal index as some environment variable or the contents of some system file. This index can be used, for example, to create replicas own subdirectories if all replicas use the same host path as bind mounts.

@languitar
Copy link

What is the solution for this request?

@vlad-vinogradov
Copy link
Author

@languitar
First of all, keep in mind that Compose does not guarantee a strictly continuous sequence of ordinal indexes. Continuity can be broken when removing some container instances and re-executing docker-compose up --scale ...

The ordinal index can be retrieved from container's FQDN.
For example, if the container has bash, nslookup, hostname, and head utilities, then try the following command:

echo "$( v="$( nslookup "$( hostname -i )" | head -n 1 )"; v="${v##* = }"; v="${v%%.*}"; v="${v##*-}"; v="${v##*_}"; echo "$v" )"

The ordinal index can also be retrieved from Docker API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants