How to access build secrets in Dockerfile

The documentation as of 2023 Nov Build Secrets · Fly Docs is not specific about a few tricky aspects.
Hope this snippet inside Dockerfile can help someone:

RUN --mount=type=secret,id=MY_SECRET,required=true \
   SHELL_MY_SECRET="$(cat /run/secrets/MY_SECRET)" && echo "SHELL_MY_SECRET is available ONLY in this line: $SHELL_MY_SECRET" 

RUN echo "Here on another line, the secret file is NO LONGER available: $(cat /run/secrets/MY_SECRET)" && echo "Also, SHELL_MY_SECRET is NO LONGER available: $SHELL_MY_SECRET"

When invoking docker build command, of course, we must tell Dockerfile the exact value of MY_SECRET. We can do so by syntax:

docker build -t foo_image_name . --progress=plain --no-cache --secret id=MY_SECRET,src=$HOME/my_computer_dir/MY_SECRET8.txt

Note: as of Nov 2023, when building image, we won’t see console outputs when we echo. I do not know how to ask fly command to show outputs, does anyone know? So in order to see outputs, I use docker command directly:

The --progress=plain --no-cache flags are saying “display console outputs”. Source: Dockerfile : how-to output the result of a command when building an image - DEV Community

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.