Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use label in docker-compose.yml, can't environment do the job?

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

like image 234
Qiulang 邱朗 Avatar asked Nov 13 '17 10:11

Qiulang 邱朗


People also ask

Can you use environment variables in docker-compose?

Configure Compose using environment variables Several environment variables are available for you to configure the Docker Compose command-line behavior.

Why you shouldn't use docker-compose in production?

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.

Does docker-compose override environment variables?

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.


2 Answers

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

  1. 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.
  2. 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.

like image 95
sumitsinghdeode Avatar answered Sep 25 '22 13:09

sumitsinghdeode


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")

re: labels vs. environment variables

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.

like image 29
Jesse Chisholm Avatar answered Sep 24 '22 13:09

Jesse Chisholm