A build secret that does not end up in a layer

Passing a private repository token as a build argument puts it in the image history, where docker history will read it back out for anyone with the image.

# leaks: visible in docker history forever
# ARG COMPOSER_AUTH
# RUN composer install

# does not leak: mounted for one instruction, never written to a layer
RUN --mount=type=secret,id=composer_auth 
    COMPOSER_AUTH="$(cat /run/secrets/composer_auth)" 
    composer install --no-dev

# docker build --secret id=composer_auth,src=./auth.json .

Deleting the file in a later RUN does not help, because layers are additive and the earlier one still contains it. The secret mount exists for exactly this and is available only during the instruction that declares it. An SSH agent mount does the same job for a private git dependency, which is the other common case and the one people usually solve by baking a deploy key into the image.