Dockerfile Labels

Overview

What I want to achieve is dynamically defining some values and passing them to the docker build to populate labels in the container. This to be used in a CICD pipeline.

Using Arguments

Here is a sample of dockerfile to build a hugo site. ARG represent arguments a default value can be set to use when no values are provided during the build of the image. It s important to have the Global definition at the top, then to have the definition between the FROM and the LABEL. LABEL is used to define key value pairs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Setup GLOBAL Arguments values
ARG VERSION="0.0.0"
ARG PROJECTNAME="unknown"
ARG DATE="1907-07-07T07:07:07Z"
ARG URL="http://documentation.com/project"

# Build Container
FROM alpine:latest AS build

# Install the Hugo go app.
RUN apk add --update hugo
ARG HUGO_VERSION="0.133.1"
ARG BASE_URL="http://localhost:8001/"
ENV BASE_URL=$BASE_URL
WORKDIR /src

COPY . /src

# Run Hugo to build the static website
RUN hugo --minify --baseURL $BASE_URL

# Base Container
FROM nginx:alpine AS base
ARG VERSION
ARG PROJECTNAME
ARG URL
ARG DATE
LABEL "website.name"="Hello Hugo website"
LABEL "website"=${PROJECTNAME}
LABEL "org.opencontainers.image.source"=${URL}
LABEL "org.opencontainers.image.created"=${DATE}
LABEL "org.opencontainers.image.version"=${VERSION}
LABEL "org.opencontainers.image.title"=${PROJECTNAME}

# Set workdir to Nginx html directory
WORKDIR /usr/share/nginx/html

COPY --from=build /src/public .

EXPOSE 80/tcp

# Start NGinx
CMD ["nginx", "-g", "daemon off;"]

The above script can be run with the following command :

1
2
3
docker build -t hello-hugo:"0.0.4"
 --build-arg VERSION="0.0.4" --build-arg DATE="2024-11-03" --build-arg
 PROJECTNAME="Hello Hugo" .

Standard Labels

During my research I have found out that some label keys were already used and suggested. (org.opencontainers.image and org.label-schema prefixes)

org.opencontainers.image prefixorg.label-schema prefixDescription
createdbuild-datedate and time on which the image was built
authors-contact details of the people or organization responsible for the image
urlurlURL to get documentation on the image
documentationusageURL to get documentation on the image
sourcevcs-urlURL to get source code for building the image
versionversionversion of the packaged software, MAY match a label or tag in the source code repository
vendorvendorName of the distributing entity, organization or individual
licenses-License(s) under which contained software is distributed as an SPDX License Expression
ref.name-Name of the reference for a target (string). SHOULD only be considered valid when on descriptors on index.json within image layout. Character set of the value SHOULD conform to alphanum of A-Za-z0-9 and separator set of -.:@/+ The reference must match the following grammar: ref ::= component ("/" component)* component ::= alphanum (separator alphanum)* alphanum ::= [A-Za-z0-9]+ separator ::= [-.:@+]
titlenameHuman-readable title of the image
descriptiondescriptionHuman-readable description of the software packaged in the image
-schema-version
-docker.*, rkt.*

References