Harvesting info from binaries in the current image building stage

Hi all!

I want to share with you a problem whose solution has been avoiding me the last days.

I need to harvest some information from the binaries during the building of an image to put it on his LABELS at the final stage of the build.

For example, I build from nodejs base image and need to recover the node, npm and yarn versions to feed the labels.

Is there any way to do that?

Here left a Dockerfile example to show what i’m trying now without any results.

Thanks for the time and help.

PS: Sorry for my bad English

Dockerfile Example
FROM docker.scm.primary/docker:18.06.1 as docker-bin
FROM docker.scm.primary/node:10.12-alpine

###################### Tools #####################
COPY --from=docker-bin /usr/local/bin/docker /usr/local/bin/

RUN apk --no-cache add \
        git \
        openssh-client
##################################################

# This is not valid, but it is what I need or similar
##################################################
ENV NODE_VERSION $(node --version)
ENV YARN_VERSION $(yarn --version)
ENV NPM_VERSION $(npm  --version)
##################################################

################### Don't move ###################
ARG BUILD_DATE
ARG BUILD_VCS_REF
ARG BUILD_VERSION
ARG BUILD_PROJECT_URL
ARG BUILD_COMMITER_NAME
ARG BUILD_COMMITER_MAIL

LABEL com.label.dummy.license=GPL-3.0 \
            com.label.dummy.maintainer="Dummy Team <dummy@dummy.com>" \
            com.label.dummy.build-date=${BUILD_DATE} \
            com.label.dummy.node.version=${NODE_VERSION} \
            com.label.dummy.yarn.version=${YARN_VERSION} \
            com.label.dummy.npm.version=${NPM_VERSION} \
            com.label.dummy.vcs.url="${BUILD_PROJECT_URL}" \
            com.label.dummy.vcs.ref.sha=${BUILD_VCS_REF} \
            com.label.dummy.vcs.ref.name=${BUILD_VERSION} \
            com.label.dummy.vcs.commiter="${BUILD_COMMITER_NAME} <${BUILD_COMMITER_MAIL}>"
##################################################