I want to store more data about my DockerImage in the docker labels for the image. Namely I want to store the python requirements that are installed in the image into the labels'. The high level of what I want to achieve:
FROM xxxx:latest
COPY requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
LABEL python_requirements=$(RUN echo $(pip freeze))
However, naturally I can't use LABEL and RUN within the same. Is there a common way to get around this? An alternative would be that I built the virtual environment outside of the docker image and push in the pinned list of requirements there, however, would prefer if I can do everything inside the image itself.
Thanks!
`
It is not currently possible to set the LABEL, ENV, and other values from the output of a RUN step in the Dockerfile.
You can configure the label using build args:
FROM xxxx:latest
COPY requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
ARG python_requirements
LABEL python_requirements=$python_requirements
And then build with a command like:
docker build --build-arg "python_requirements=$(pip freeze)" ...
You can skip the build arg and label in the Dockerfile and add it directly during the build:
docker build --label "python_requirements=$(pip freeze)" ...
You could also modify the image after it has been built. This will change the digest of the image. Examples of tools that do that include Google's crane mutate
and my own regctl image mod
.