I am learning docker now. I am trying to figure out what kind of problem Docker label can solve.
I can understand why use label in Dockerfile, e.g add build-related metadata, but I still don't get why using it in docker-compose.yml? What is the difference between using labels vs environment? I assume there will be different use cases but I just can't figure it out.
Can someone give me some practical example?
Thanks
Configure Compose using environment variables Several environment variables are available for you to configure the Docker Compose command-line behavior.
It's a cool tool to make handling container configuration or multiple interconnected containers a bit easier. The “don't use docker-compose in production” statement is motivated by hidden assumptions which are not necessarily valid for everybody, and propagated by unclear communication.
But docker-compose does not stop at the . env and the host's current environment variables. It's cool that you can simply override values of your . env file, but this flexibility is can also be the source of nasty bugs.
docker-compose.yml is used by docker-compose utility to build and run the services which you have defined in docker-compose.yml
While working with docker-compose we can use two thing
docker-compose build
this will build the services which is defined under docker-compose.yml
but in order to run this services it has to have a image which is with docker-engine
if you do docker image ls
you find the images which is built up with the docker-compose
and inspect it there you find a label
which defines the metadata of that particular image. docker-compose up
this will run the services which is built up in docker-container build
now this running container has to have some metadata like env
this is set with enviroment
in docker-compose.yml
P.S. :- This is my first answer in stack overflow. If you didn't get just give a comment I will try to explain my best.
Another reason to use labels
in docker-compose
is to flag your containers
as part of this docker-compose suite of containers, as opposed to other purposes each docker image
might get used for.
Here's an example docker-compose.yml that shares labels across two services:
x-common-labels: &common-labels
my.project.environment: "my project"
my.project.maintainer: "[email protected]"
services:
s1:
image: somebodyelse/someimage
labels:
<<: *common-labels
# ...
s2:
build:
context: .
image: my/s2
labels:
<<: *common-labels
# ...
Then you can do things like this to just kill this project's containers.
docker rm -f $(docker container ls --format "{{.ID}}" --filter "label=my.project.environment")
Labels are only available to the docker
and docker-compose
commands on your host.
Environment variables are also available at run-time inside the docker container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With