Reputation: 67
I am trying to use variables inside a Dockerfile. I don't understand why the occurrence $MyVariable is never replaced by the value.
I build my image using:
docker build --build-arg PYTHON_VERSION=python-3.8.8.amd64 -t succeeds .
In my dockerfile I have the following code :
# escape=`
FROM mcr.microsoft.com/windows/servercore:1909
ARG PYTHON_VERSION=python-3.8.1.amd64
ENV PYTHON=C:\$PYTHON_VERSION
RUN echo $PYTHON_VERSION
RUN echo ${PYTHON_VERSION}
RUN echo PYTHON_VERSION
The shell respectively print:
$PYTHON_VERSION
${PYTHON_VERSION}
PYTHON_VERSION
The documentation suggests:
An ARG declared before a FROM is outside of a build stage, so it can’t be used in any instruction after a FROM. To use the default value of an ARG declared before the first FROM use an ARG instruction without a value inside of a build stage:
ARG VERSION=latest FROM $VERSION ARG VERSION RUN echo $VERSION > image_version
But I am clearly not getting the same result. Shall I add something in the docker command to use the variables?
Upvotes: 3
Views: 8626
Reputation: 3606
You need to know that ARG is not available in the container. While ENV is available in the container.
Please see this good diagram from here:
So in your case you need to use the ENV name and not the ARG name like this:
# escape=`
FROM mcr.microsoft.com/windows/servercore:1909
ARG PYTHON_VERSION=python-3.8.1.amd64
ENV PYTHON=$PYTHON_VERSION
RUN echo $PYTHON
Upvotes: 8