1. 程式人生 > 實用技巧 >【Docker】Dockerfile 之 LABEL

【Docker】Dockerfile 之 LABEL

參考教程:https://docs.docker.com/engine/reference/builder/

環境

  1. virtual box 6.1
  2. centos 7.8
  3. docker 19.03

LABEL

LABEL <key>=<value> <key>=<value> <key>=<value> ...

The LABEL instruction adds metadata to an image. A LABEL is a key-value pair. To include spaces within a LABEL value, use quotes and backslashes as you would in command-line parsing. A few usage examples:

LABEL 指令將元資料新增到映象。LABEL 是鍵值對。要在 LABEL 值中包含空格,請像在命令列中一樣使用引號和反斜槓。一些用法示例:

LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines."

An image can have more than one label. You can specify multiple labels on a single line. Prior to Docker 1.10, this decreased the size of the final image, but this is no longer the case. You may still choose to specify multiple labels in a single instruction, in one of the following two ways:

一個映象可以有多個標籤。您可以在一行上指定多個標籤。在 Docker 1.10 之前,這減小了最終映象的大小,但是現在不再如此。您仍然可以選擇以下兩種方式之一在一條指令中指定多個標籤:

LABEL multi.label1="value1" multi.label2="value2" other="value3"
LABEL multi.label1="value1" \
      multi.label2="value2" \
      other="value3"

Labels included in base or parent images (images in the FROM

line) are inherited by your image. If a label already exists but with a different value, the most-recently-applied value overrides any previously-set value.

基礎或父映象(FROM 行中的映象)中包含的標籤由您的映象繼承。如果標籤已經存在但具有不同的值,則最近應用的值將覆蓋任何先前設定的值。

To view an image’s labels, use the docker image inspect command. You can use the --format option to show just the labels;

要檢視映象的標籤,請使用 docker image inspect 命令。您可以使用 --format 選項僅顯示標籤;

docker image inspect --format='' myimage
{
  "com.example.vendor": "ACME Incorporated",
  "com.example.label-with-value": "foo",
  "version": "1.0",
  "description": "This text illustrates that label-values can span multiple lines.",
  "multi.label1": "value1",
  "multi.label2": "value2",
  "other": "value3"
}

示例

Dockerfile 檔案

FROM busybox
LABEL author=jiangbo
CMD echo jiangbo

構建結果

[root@master env]# docker build -t jiangbo:0.0.1 . --no-cache
Sending build context to Docker daemon  3.584kB
Step 1/3 : FROM busybox
 ---> dc3bacd8b5ea
Step 2/3 : LABEL author=jiangbo
 ---> Running in 0ff7462c023b
Removing intermediate container 0ff7462c023b
 ---> bae66ec32f55
Step 3/3 : CMD echo jiangbo
 ---> Running in 2a3701bc64fc
Removing intermediate container 2a3701bc64fc
 ---> f75b03f5ec1f
Successfully built f75b03f5ec1f
Successfully tagged jiangbo:0.0.1
[root@master env]#

檢視標籤

[root@master env]# docker image inspect jiangbo:0.0.1 --format "{{json .ContainerConfig.Labels}}"|jq
{
  "author": "jiangbo"
}

MAINTAINER

MAINTAINER 已過時,使用 LABEL 代替。

總結

介紹了 Dockerfile 中 LABEL 指令的使用。