print ARG value in docker build

Viewed 10892

Is there a way to print ARGs values, that are passed via the --build-arg flags to a docker build command?

Just using RUN echo $ARG_NAME isn't enough since I want it to be printed before the FROM section, where it's not allowed.

The point is to see these values immediately so I can stop the build quickly, preventing downloading the wrong base images.

Already searched docker docs and google. Maybe someone here can shed some light.

2 Answers

You could use a multi-stage build where the first stage is just for diagnostics and otherwise gets completely ignored.

FROM busybox
ARG ARG_NAME
RUN echo $ARG_NAME

FROM python:3.8
ARG ARG_NAME
...
CMD ["my_app"]

Note that Docker layer caching can cause RUN steps to be totally skipped so even this isn't 100% reliable.

Related