Using secret multiple times in a Dockerfile is a nuisance

The basic idea of secrets is great - thanks! - but they can be a nuisance:

I have many RUN steps in my Dockerfile that all need the same secret. Instead of declaring one ARG at the top of the Dockerfile, I know have to repeat the secret all over the place, making for some pretty distracting noise. See excerpt below.

Also, b/c of this, the --progress=tty option to docker build becomes unusable - unless I have an absolutely gigantic terminal width, the actual command for the RUN steps will be truncated from docker build’s output, and the available width will be used up by the relatively unimportant flags dealing with the secret setup. ( --progress=plain is so verbose in my case that it’s equally unusable)

Docker bake is not an option, and combining the steps into one RUN command via the shell && operator is not a clean solution IMO.


RUN --mount=type=secret,id=AWS_ACCESS_KEY_ID,env=AWS_ACCESS_KEY_ID \
    --mount=type=secret,id=AWS_SECRET_ACCESS_KEY,env=AWS_SECRET_ACCESS_KEY \
    step1
RUN --mount=type=secret,id=AWS_ACCESS_KEY_ID,env=AWS_ACCESS_KEY_ID \
    --mount=type=secret,id=AWS_SECRET_ACCESS_KEY,env=AWS_SECRET_ACCESS_KEY \
    step2
RUN --mount=type=secret,id=AWS_ACCESS_KEY_ID,env=AWS_ACCESS_KEY_ID \
    --mount=type=secret,id=AWS_SECRET_ACCESS_KEY,env=AWS_SECRET_ACCESS_KEY \
    step3
...

You don’t have to use && . Even official images use shell options like this:

It still uses backslashes, but you can avoid those if using the heredoc syntax

https://docs.docker.com/reference/dockerfile/#here-documents

# syntax=docker/dockerfile:1
FROM debian
RUN <<EOT bash
  set -ex
  apt-get update
  apt-get install -y vim
EOT

Regarding progress formats, I understand the problem and a more compact format could be a feature request, but I wrote script that shows only the logs. It is based on the “rawjson” output format

docker buildx build --secret id=aws,src=test.txt . -t localhost/test \
  --progress rawjson --no-cache &>/dev/stdout \
  | jq -r '.logs | select(. != null) | .[].data' \
  | base64 -d

You could use something like this and with a more complex jq filter you could also include other metadata as well if needed. It is just for demonstrating the rawjson format. Here is my output:

Collecting awscli
  Downloading awscli-1.42.28-py3-none-any.whl.metadata (11 kB)
Collecting botocore==1.40.28 (from awscli)
  Downloading botocore-1.40.28-py3-none-any.whl.metadata (5.7 kB)
Collecting docutils<=0.19,>=0.18.1 (from awscli)
  Downloading docutils-0.19-py3-none-any.whl.metadata (2.7 kB)
Collecting s3transfer<0.15.0,>=0.14.0 (from awscli)
  Downloading s3transfer-0.14.0-py3-none-any.whl.metadata (1.7 kB)
Collecting PyYAML<6.1,>=3.10 (from awscli)
  Downloading PyYAML-6.0.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.metadata (2.1 kB)
Collecting colorama<0.4.7,>=0.2.5 (from awscli)
  Downloading colorama-0.4.6-py2.py3-none-any.whl.metadata (17 kB)
Collecting rsa<4.8,>=3.1.2 (from awscli)
  Downloading rsa-4.7.2-py3-none-any.whl.metadata (3.6 kB)
Collecting jmespath<2.0.0,>=0.7.1 (from botocore==1.40.28->awscli)
  Downloading jmespath-1.0.1-py3-none-any.whl.metadata (7.6 kB)
Collecting python-dateutil<3.0.0,>=2.1 (from botocore==1.40.28->awscli)
  Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl.metadata (8.4 kB)
Collecting urllib3!=2.2.0,<3,>=1.25.4 (from botocore==1.40.28->awscli)
  Downloading urllib3-2.5.0-py3-none-any.whl.metadata (6.5 kB)
Collecting six>=1.5 (from python-dateutil<3.0.0,>=2.1->botocore==1.40.28->awscli)
  Downloading six-1.17.0-py2.py3-none-any.whl.metadata (1.7 kB)
Collecting pyasn1>=0.1.3 (from rsa<4.8,>=3.1.2->awscli)
  Downloading pyasn1-0.6.1-py3-none-any.whl.metadata (8.4 kB)
Downloading awscli-1.42.28-py3-none-any.whl (4.7 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.7/4.7 MB 30.1 MB/s  0:00:00
Downloading botocore-1.40.28-py3-none-any.whl (14.0 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.0/14.0 MB 55.9 MB/s  0:00:00
Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Downloading docutils-0.19-py3-none-any.whl (570 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 570.5/570.5 kB 39.6 MB/s  0:00:00
Downloading jmespath-1.0.1-py3-none-any.whl (20 kB)
Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB)
Downloading PyYAML-6.0.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (733 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 733.4/733.4 kB 60.3 MB/s  0:00:00
Downloading rsa-4.7.2-py3-none-any.whl (34 kB)
Downloading s3transfer-0.14.0-py3-none-any.whl (85 kB)
Downloading urllib3-2.5.0-py3-none-any.whl (129 kB)
Downloading pyasn1-0.6.1-py3-none-any.whl (83 kB)
Downloading six-1.17.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: urllib3, six, PyYAML, pyasn1, jmespath, docutils, colorama, rsa, python-dateutil, botocore, s3transfer, awscli

Successfully installed PyYAML-6.0.2 awscli-1.42.28 botocore-1.40.28 colorama-0.4.6 docutils-0.19 jmespath-1.0.1 pyasn1-0.6.1 python-dateutil-2.9.0.post0 rsa-4.7.2 s3transfer-0.14.0 six-1.17.0 urllib3-2.5.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.
NOthing

If you want to ask for a feature, you can do it in the roadmap

Thanks for the feedback. I should’ve been more specific - it’s not just the shell && operator but any sort of combining multiple steps into one RUN command is not a good option for us. Doing this would a) ruin intermediate layer caching and b) make parallelization (multi-stage) difficult/impossible. We’re building different software packages in all the various RUN steps - so both those features are important.
I’ve requested a feature as you mentioned, let’s see what becomes of it. I’ll try out the json log parsing if we get too drowned in the output from --progress-plain - thanks

Thank you for creating the feature request. I share the link here so anyone can find it easily who would like to join the converstation there

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