why docker change value of args if put in FROM - Stack Overflow

admin2025-04-30  0

for the dockerfile ->

ARG PYTHON_VERSION=3.12
FROM python:$PYTHON_VERSION-slim
RUN echo $PYTHON_VERSION

the build ->

docker build --progress=plain  --no-cache

show ->

#5 [XXX 2/2] RUN echo 3.12.8
#5 0.200 3.12.8

why the ARG change from 3.12 to to 3.12.8 and can I disable it ?

for the dockerfile ->

ARG PYTHON_VERSION=3.12
FROM python:$PYTHON_VERSION-slim
RUN echo $PYTHON_VERSION

the build ->

docker build --progress=plain  --no-cache

show ->

#5 [XXX 2/2] RUN echo 3.12.8
#5 0.200 3.12.8

why the ARG change from 3.12 to to 3.12.8 and can I disable it ?

Share Improve this question asked Jan 4 at 22:42 raphaelauvraphaelauv 9881 gold badge13 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

Build args are scoped to either the remaining steps of the defined stage, or when specified at the top of the Dockerfile to only the global scope (the FROM lines, and not any of the steps within a build stage).

So there is no PYTHON_VERSION arg within the stage, and docker falls back to using environment variables defined in the selected image:

$ docker inspect python:3.12-slim
[
    {
...
        "Config": {
...
            "Env": [
                "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "LANG=C.UTF-8",
                "GPG_KEY=7169605F62C751356D054A26A821E680E5FA6305",
                "PYTHON_VERSION=3.12.8",
                "PYTHON_SHA256=c909157bb25ec114e5869124cc2a9c4a4d4c1e957ca4ff553f1edc692101154e"
            ],
...
        },
...

To reuse the build arg within the stage, you can define the arg again there:

ARG PYTHON_VERSION=3.12
FROM python:$PYTHON_VERSION-slim
ARG PYTHON_VERSION
RUN echo $PYTHON_VERSION
转载请注明原文地址:http://www.anycun.com/QandA/1746025040a91507.html