-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Implemented labels on docker compose #1124
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
Conversation
|
Thanks! Wondering; would it make sense to use a dict/hash-map (whatchamacallit in yaml) in stead of an array? I.e; web:
labels:
ram: 2g
storage: ssd |
|
@thaJeztah That would overwrite the settings in the yaml file, described here moby/moby#11187 (An end-to-end example) |
|
I like that. Wrt hash/array; I'll leave that to the maintainers for now. both will work, but a hash more closely matches the JSON in the Docker API Also, the docker labels feature supports a |
|
It was mentioned a few times in the labels PR that labels pretty closely resembles environment variables. I suspect that will be true for our implementation here as well. For environment variables both lists and dicts are supported in the config, I could see labels working the same way: |
compose/service.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not be namespacing labels here, it should be left up to the developer.
We will likely use a namespace for our internal labels in #1066, but any user specified labels should be unmodified I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed; labels set by the user should be sent to docker as-is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A motivation for a separate prefix for labels specified in the composition definition I can see is to be able to differ between the labels specified in the composition and the labels specified with docker-compose run --label.
This implements separation of concern between labels specified by the developer and whoever is running the composition in a particular environment. Otherwise docker-compose run invocations are required to always specify and overwrite all possible labels used in an environment if the guy running the workloads wants to avoid a developer mistakenly specifying a label used for operational concerns.
Simple example:
You are using a label profile=production which influences resource allocation and scheduling. You do not want compositions to inherit these privileges unless the label was specified by the operator invoking docker-compose run.
A richer write-up of this separation of concern can be found here:
moby/moby#11187
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, labels used for scheduling (Swarm) should be namespaced and perhaps not even defined as labels in docker-compose.yml (even though they will be set as labels), e.g.
web:
constraints:
storage: ssd
memory: 2g
labels:
hello: worldthe "constraints" will then result in a --label com.docker.swarm.constraint=storage=ssd (or similar)
I think Swarm is planning on namespacing their labels as well, but I'll have to check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed that the constraints even though stored as labels should not all share the same namespace. It must be known what is a constraint, what is an identification label, what is a capability label, etc.
An operator should be able to say at docker-compose run time to ignore all previous constraints and use a new set. This should not require the operator to overwrite each constraint separately. Allowing for this separation of concern is absolutely critical. It must be possible to tie applications to infrastructure without storing this glue in the application itself.
So instead of separating compose and runtime labels with a namespace, an alternative could be a --remove-labels option which allows for wildcards:
web:
labels:
constraints:
storage: ssd
memory: 2
generic:
hello: worldTo overwrite all constraints:
[...] --remove-labels 'constraints.*' --label constraints.memory=4g
True. The reason I thought a dict would be more appropriate because (contrary to env-variables), the storage for "labels" in Docker also is a dict. Label keys (names) are unique and a dict more verbosely expresses that. An array should work, but would (probably) require more handling, because latter values for the same label should overwrite former ones.
Funny fact; Docker actually supports multiple env-files. The contents of those files are combined / latter values overwrite former values here as well. I was wondering if it should be possible to specify a |
|
Thanks for the feedback guys. The problem I have is, for example, with this command: Now, is the I can also create an option to read from a .yml that has the labels predefined by the operator. web:
constraints:
storage: ssd
memory: 2g
labels:
hello: worldThat would result in something like: "Labels": {
"io.docker.compose.cmdline:memory": "2g" -> The one inside labels.yml from the OP
"io.docker.compose.cfgfile:memory": "99g" -> The one inside compose.yml from the dev
},What are your thoughts on this? |
|
@aanm
I'm sure @aanand has his own thoughts on this ;-) |
Signed-off-by: André Martins <martins@noironetworks.com>
$ sudo docker-compose help up
...
--labels-file FILE Uses all labels, in the given yaml FILE for the
SERVICEs to be up.
--no-prefix-labels Don't prefix '.cfg' and '.cmd' when --labels-file
is used.
--remove-labels [REGEX] Removes all labels. If REGEX is given, removes
labels that match the given REGular EXpression.
(Note: This option has less priority than the
given --labels-file)
...
$ sudo docker-compose help run
...
--labels-file FILE Uses all labels, in the given yaml FILE for the
SERVICE to be run.
--no-prefix-labels Don't prefix '.cfg' and '.cmd' when --labels-file
is used.
--remove-labels [REGEX] Removes all labels. If REGEX is given, removes
labels that match the given REGular EXpression.
(Note: This option has less priority than the
given --labels-file)
...
$ cat docker-compose.yml
web:
labels:
dev.attrib.color: white
dev.attrib.shape: square
build: .
...
redis:
labels:
storage: ssd
ram: 4g
profile: production
image: redis
$ cat labels.yml
web:
labels:
color: green
redis:
labels:
storage: ssd
ram: 2g
profile: production
$ sudo docker-compose up --labels-file labels.yml --remove-labels 'dev.*'
$ sudo docker inspect dockercompose_web_1 | grep Labels -B1 -A6
"Image": "dockercompose_web",
"Labels": {
".cmd:color": "green"
},
"MacAddress": "",
"Memory": 0,
"MemorySwap": 0,
"NetworkDisabled": false,
$ sudo docker inspect dockercompose_redis_1 | grep Labels -B1 -A8
"Image": "redis:latest",
"Labels": {
".cfg:profile": "production",
".cfg:ram": "4g",
".cfg:storage": "ssd",
".cmd:profile": "production",
".cmd:ram": "2g",
".cmd:storage": "ssd"
},
"MacAddress": "",Note the absence of |
|
Thanks @aanm! Hm, my thoughts;
Not sure if you meant write that, but the example should be I like the idea, though, of using a Some other thoughts;
Perhaps more importantly; I'm fine with adding more features later, but please in a follow-up PR. Keep the initial implementation simple and only implement what's supported by Docker itself, i.e.;
Those should not require a lot of discussion, which makes it easier to merge. In a follow-up/separate PR, All features wrt moby/moby#11187 etc., should be in a follow up. Currently, moby/moby#11187 is just a proposal and it doesn't make sense to implement anything before it's even accepted in Docker itself. It's fine to discuss options for implementing it in Compose, but best kept in a separate issue. However, I'm not a maintainer, just my personal opinion! |
Yeah, I agree, it doesn't look "good" with .cfg/.cmd. Since docker uses CLI > YAML I'll do it the same way.
What's a "newline-delimited file"? This -> https://en.wikipedia.org/wiki/Line_Delimited_JSON#Example_Output
Okay ;-) I will:
Thanks for the feedback! |
The label file uses the same format as Basically, it's just a single
I would personally move it to a new PR, yes, so that it can be discussed without upholding this PR. But again, I'm not a maintainer; they could have a different opinion here :) |
|
Who consumes (or acts) upon these labels? From what I understand so far, from this PR (and also from moby/moby#9882), I see a way for opaque labels to be passed from the compose yaml spec or from the dockerfile all the way to the docker daemon. But since these are opaque, docker itself is not acting on the labels other than preserving them. So my question is, who are these labels intended for and how do we go about acting on them? My interest for example would be around getting the storage specific labels and making the storage implementation honor the requested labels. |
|
At this moment, labels are only that; a label. No software "acts" on them. Docker enables filtering images/containers based on labels and you are able to read the labels using It is possible to have software make use of those labels (e.g. Composer itself, to store the project name, or Swarm to schedule containers), but for now that should be "out of scope" for this PR. |
|
Thanks for making a start on this. I think this PR does much more than the minimum necessary for a useful labels feature:
Furthermore, it puts a lot of logic in I've made a start on an MVP labels feature in #1139. I think the next good thing to implement would be |
yml example file
Do not merge yet.
Depends on docker/docker-py#529
Signed-off-by: André Martins martins@noironetworks.com