问题
Where can I find out which template values are available in my Docker UCP Swarm cluster?
With template values I mean things like this https://docs.docker.com/engine/reference/commandline/service_create/#create-services-using-templates.
I get the feeling that the tree I'm traversing looks a bit like docker inspect
output, but a smaller set of it. Which keys? and which expressions can I use?
Besides that, I often get errors like for {{.Engine.Labels}}
<.Engine.Labels>: can't evaluate field Engine in type *template.Context
So, it seems the context in which the tree is stored is 'Context'. Which is not a docker concept as far as I know.
An example of how I'm trying to use this:
version: "3.7"
services:
bar:
image: foo/bar:latest
environment:
- hostname={{.Node.Hostname}}
deploy:
replicas: 2
In docker inspect
there is also a --format
flag where you can use the same go-template syntax, but it is not the tree that can be used in the docker stack setup.
回答1:
I believe that context.go in the swarmkit repo is responsible.
This means you have the following:
Service struct {
ID string
Name string
Labels map[string]string
}
Node struct {
ID string
Hostname string
Platform Platform
}
Task struct {
ID string
Name string
Slot string
}
e.g. .Service.ID
,.Service.Labels.LabelNameGoesHere
, .Task.Name
etc.
I was hoping that the Node labels would be exposed and accessible for the templating the docker-compose.yml, but alas not.
来源:https://stackoverflow.com/questions/54251442/which-docker-template-values-are-available-in-docker-stack-deploy-compose