-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Setting container hostname to service_name-xx
instead of container-id
#9858
Comments
Set the services:
my-app:
image: my-image:0.1
hostname: my-app-1
his-app:
image: his-image:0.1 |
Oops! The description I gave made it look as 'question: how to set hostname for container' while what wanted was: Is it possible to enhance compose to set hostname to my-app-1 for containers without explicit hostname configuration in compose file, instead of container-id. And in case of multiple replicas set as my-app-1, my-app-2 etc. for the replicas. |
Ah, ok, I'll re-open and mark this as a feature request. Is there a specific reason that the built-in name resolution is inadequate and you want the hostname explicitly set? Trying to understand the use case here. |
Thanks @milas. Explanation follows, but before that would like to mention that this may be a specific use case, as I have not much idea if there are other systems, that rely on hostname like rabbitmq. Came across this when trying to cluster rabbitmq using its classic peer discovery, using compose. RabbitMQ configuration
compose file
For 2 instances 2 services, for 5 instances 5 service definitions. No benefit of scaling can be taken here. If compose could set hostname
And peer discovery configured as
We can use |
Awesome, thanks so much for the detailed explanation! I can't guarantee we will take this feature, but having well-defined use cases helps us prioritize requests. For anyone else who has reached this issue and is interested, please use the 👍 reaction on the main issue. If you have other use cases not covered, please feel free to comment as well. |
Some possible implementation notes: As of now, this is what the spec has to say on
Arguably, this is already underspecified for usage with At a first glance, I'd say we should he appending the replica # (if > 1) to the custom hostname (if provided) to prevent duplicates. Then you could set something like Any bigger change (e.g. to allow a templating variable, maybe similar to |
@milas the issue is closed, could you please re-open it. |
Sorry for interrupt, but maybe ability to set a pattern for hostname may solve the problem?
x-templates:
common: &x-common
env_file: .env
hostname: 'my-{{ .Service.Name }}'
# etc.
services:
service1:
<<: *x-common
# etc.
service2:
<<: *x-common
# etc.
service3:
<<: *x-common
# etc. and get as a result Some kind of "helmy" templating, i would say |
We really, really don't want to introduce a templating language inside the compose file format |
@milas we indeed could compute hostname as container is created with some custom, dedicated logic for this use-case I wonder which character to use to prevent any side effet, and make it clear a replacement will take place here (as I can guess we will get requests to support this in a few other places) |
I ran a quick test: services:
base:
image: nginx
scale: 2
master:
image: alpine
command: ping localhost ran the stack then exec into my "master" node:
service container is registered on network with alias {{project-service-replica}} you can use for service discovery as requested, without any additional configuration or explicit hostname |
just a quick note that container index might not start by |
not now it is supported natrually? |
Do you have a use case for this feature you could describe here ? |
Here's the scenario: In most cases, cluster software is designed as a single process. Sometimes it utilizes all hardware resources, but often there are times when it's blocked(Networking, IO, Sync.... etc). Here's the structure: Master Node -> [Docker Compose (Host Machine)] -> Slave Nodes (Container). In this setup, Docker Compose acts as an intermediate level manager node. However, there's no information about the original software's host machine. So, I'd like to add metadata to the slave nodes regarding their host machine. For example: Host1-1 Host2-1 Of course, this can be achieved through scripting, but it's a bit complex. (I have already solved this problem... just using scripting.) I think if Docker Compose supported more systemable environment variables(like service name, replica index, etc...), it would simplify the problem. (Almost the way is good. of course, more complex things have to use theres way) |
IIUC you'd like to get some metadata in container to let you know about the service and replica number being ran. Can you please describe how you would use this, and why this can't just be achieved by external monitoring (which has easy access to container labels)? |
Perhaps this could use the templating option as they're supported by swarm services https://docs.docker.com/engine/reference/commandline/service_create/#create-services-using-templates |
before considering feasability and syntax to support this, I'd like to understand the benefits having such a feature. Actually I'd prefer we get a generic introspection mechanism for a container to know about it's usage context, I would typically suggest we rely on the legacy |
Yeah, introspection is a topic that returns frequently, but still needs a lot of thinking. The templating options mean that the user remains in control over what information "leaks" into the container. (There were some (probably similar) use-cases for users running swarm-services). I agree that we should have a good understanding of use-cases before committing to anything (but thought I'd comment the suggestion so that IF we would consider, we could re-use existing principles, instead of creating something new "from scratch'). |
Actually I am trying to use Azure Pipeline Agent on Host machine, the agent is designed to perform 1 task at 1 time. So I run this with docker compose. 1 host machine, 4 agent docker containers. If some agent dies, then I have to check the agent is where on it. |
@dbwodlf3 are you running those agents as replicas for a single service? How would |
Closing this feature request as "not planed" as there's no clear use-case requiring such a change. Feel free to open a follow-up issue if you have one you can share |
@ndeloof, please close again as not planned. The purple colors indicate that it is closed as done. |
@ndeloof could you please reopen this? IMHO it could be solved with a setting in the deploy section something like "set-hostname: true" which set the hostname of each node. |
@womblep as commented on #9858 (comment) replicas can be accessed as |
@ndeloof yes setting the hostname does help. RabbitMQ expects that the reverse-dns name is the same as the hostname to be able to resolve cluster members when setting up the cluster using the DNS peer discovery protocol.
|
@womblep that's a weird requirement by rabbitmq, is this documented anywhere ? According to https://www.rabbitmq.com/docs/cluster-formation#peer-discovery-dns there's no "see if the 2 match" constraint you describe |
I also have an usecase for this. I am running a local slurm cluster using docker compose. For this to work, the compute node which is starting the daemon needs to be aware of its hostname, which I cannot do if the compute-node service has a When running with This year Slurm added a kind of "autodiscovery" service as mentioned here, but unfortunately I am stuck on an older version for compatibility with other systems. In any case, these kinds of issues seems recurrent when building different kinds of clusters, as previously exemplified in this thread. |
@mfontana-elem you're basically asking for moby/moby#2335 which has been rejected by maintainers. |
Thanks for the link. That is one possibility. The other one being |
Thanks for the link. That is one possibility. The other one being For the time being, just in case it helps someone in the same circustances, I resorted to adding this very ugly line in my entrypoint with which I wouldn't be able to sleep if this wasn't for a local development environment: for IP in $(hostname -I); do
echo -e "$IP\t$(hostname)$(dig -x $IP +short | cut -d'.' -f1 | xargs)" >> /etc/hosts
done (This also force me into adding |
It is useful feature when deal with docker swarm seems has '{{.Service.Name}}-{{.Task.Slot}}' things |
For this sample compose file
Compose creates my-app-1 container for my-app with host-name as container-id say
rndmcntrid
This container is reachable from other services like
his-app-1
withmy-app
,my-app-1
orrndmcntrid
.Can we have compose set container hostname as
service-1
? e.g. in case of my-app above, hostname be set tomy-app-1
instead ofrndmcntrid
The text was updated successfully, but these errors were encountered: