RUN export don't work as expected

Hello,

I have a question.

When i build a file like that :

FROM debian

RUN export A="b"
RUN echo "Variable A = $A";

And I build the file. It gives me that :

$ docker build --progress=plain .
#0 building with "default" instance using docker driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 94B done
#1 DONE 0.0s

#2 [internal] load metadata for docker.io/library/debian:latest
#2 DONE 0.3s

#3 [internal] load .dockerignore
#3 transferring context: 2B done
#3 DONE 0.0s

#4 [1/3] FROM docker.io/library/debian:latest@sha256:45f2e735295654f13e3be10da2a6892c708f71a71be845818f6058982761a6d3
#4 DONE 0.0s

#5 [2/3] RUN export A="b"
#5 CACHED

#6 [3/3] RUN echo "Variable A = $A";
#6 0.298 Variable A = 
#6 DONE 0.3s

#7 exporting to image
#7 exporting layers 0.0s done
#7 writing image sha256:00991171fd9aa6c32d3a736e1382a38490703526a685a804ea252ae8420a0e5b done
#7 DONE 0.0s

You can see here #6 0.298 Variable A = that the variable A is empty, but I export it just one RUN before.

Is the RUN instruction begin a new shell in intern ? Is it a bug ?

Thank you for your answer,
Novalis10

Even better: every RUN instructions runs in its own temporary container. Neither running processes, nor environment variables will be retained after the instruction.

You either need to execute all commands in a single RUN instruction and chain them via && or ; (depending on what you need), or use ENV instructions to declare variables that will be available.

1 Like

Just to add, in case the variable is not needed for the image itself, but just the build process

For build-time arguments, use ARG, for run-time environment variables, use ENV

Thank you, I understand better now.