Using ENV and ARG instructions in a Dockerfile to handle secrets can expose sensitive information through the image
metadata, container environment, or build logs.
The ARG and ENV instructions in a Dockerfile configure the image build and the container environment respectively. When
either instruction is used to pass secrets, traces of those values are typically retained — in the image layer history, the container environment, or
the build logs. Any party that gains access to the image or its build history can retrieve these values.
The concrete impact depends on the nature of the exposed secret:
FROM example
ARG ACCESS_TOKEN # Noncompliant
ENV ACCESS_TOKEN=${ACCESS_TOKEN} # Noncompliant
CMD /run.sh
For runtime secrets, leave environment variables empty in the Dockerfile and inject their values at container start. Store secrets in an environment file and start the container with the --env-file argument:
FROM example ENV ACCESS_TOKEN="" CMD /run.sh
docker run --env-file .env myImage
For build-time secrets, use Buildkit’s secret mount type instead:
FROM example RUN --mount=type=secret,id=build_secret ./installer.sh