Can I set docker container labels based on output of previous run command during build?
Asked Answered
H

2

17

I wonder if is possible to set a LABEL during building of a container with a based that is based on a command run on the container.

Example, wanting to expose python=2.7.14 when the number would be obtained from running python --version during build.

The idea is to expose some information about what was build as labels, before starting the build these versions are not known (like packages installed using yum).

Hailey answered 19/4, 2018 at 16:48 Comment(3)
This would be really useful and seems like a natural requirement in multi-stage builds. Are you aware of any changes in the meantime, or feature requests?Wallah
Looking to do pretty much exactly this right now, take it neither of you got anywhere @Wallah @Sorin?Amimia
You could give a try to this approach. Note, I am not sure if this would suffice your requirement. There might be a way to get the value of version and write it to some file (and keep the file in host volume). Use this file to set the label to container in next run by using the following command $ docker run --label-file ./labels ubuntu bash docker run with labelsAcantho
C
1

Seems like it is just not possible. You need to use build arguments before launching the build.

Cobia answered 12/12, 2020 at 18:11 Comment(0)
C
0

You can build it once, then extract whatever labels you need by running commands against the built image and then rebuild by adding --label flags. Since the build is cached, everything you built will remain the same.

This what I've come up with in some CI job of mine

date_tag=$(date +%Y-%m-%d)
image=$CI_REGISTRY/$CI_PROJECT_PATH/devops-runner

# Log in and build the image both as latest and with the current date
docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN $CI_REGISTRY
docker build \
  -t $image:latest \
  -t $image:$date_tag \
  docker/

# Extract the versions of the tools installed in the image
debian_version=$(docker run --rm $image -c "cat /etc/debian_version")
aws_cli_version=$(docker run --rm $image -c "aws --version" | awk '{print $1}' | cut -d/ -f2)
terraform_version=$(docker run --rm $image -c "terraform --version" | head -n 1 | awk '{print $2}')
kubectl_version=$(docker run --rm $image -c "kubectl version --client --short" | awk 'NR==1{print $3}')
helm_version=$(docker run --rm $image -c "helm version --short" | awk '{print $1}')

# Rebuilding with the cache from the last step but adding labels to image
docker build \
  -t $image:latest \
  -t $image:$date_tag \
  --label "debian_version=${debian_version}" \
  --label "aws_cli_version=${aws_cli_version}" \
  --label "terraform_version=${terraform_version}" \
  --label "kubectl_version=${kubectl_version}" \
  --label "helm_version=${helm_version}" \ 
  docker/

# Push the image to the registry
docker push $image:latest
docker push $image:$date_tag

# Log the image metadata
docker inspect --format='{{json .Config.Labels}}' $image:latest | jq .
Cuneiform answered 10/7 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.