Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Docker printing out empty string instead of a value set during build

This is my dockerfile:

FROM python:3.11-slim-bullseye

COPY requirements.txt .
RUN python -m pip install --upgrade pip
RUN python -m pip install -r requirements.txt

ARG my_build_arg

CMD echo $my_build_arg

I want to set an argument at the time of building the image. So, I run this command:

docker build --build-arg my_build_arg='my_fantastic_value' -t 'my_image' .

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When I run docker run my_image into the console, I am expecting that docker will print out ‘my_fantastic_value’. Instead, docker prints a newline with no text. What is wrong here?

>Solution :

ARG instructions do not persist in the image. You can use an ENV instruction instead, which does persist in the image:

...
ARG my_build_arg
ENV my_build_env=$my_build_arg
CMD echo $my_build_env

See more here: https://docs.docker.com/engine/reference/builder/#using-arg-variables

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading