# RUN export don't work as expected

**URL:** <https://forums.docker.com/t/run-export-dont-work-as-expected/142869>\
**Category:** General\
**Created:** [August 1, 2024, 4:46pm UTC](https://forums.docker.com/t/run-export-dont-work-as-expected/142869 "2024-08-01T16:46:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![novalis10](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/novalis10/32/49834_2.png) [@novalis10](https://forums.docker.com/u/novalis10)\
**Post date:** [August 1, 2024, 4:46pm UTC](https://forums.docker.com/t/run-export-dont-work-as-expected/142869/1 "2024-08-01T16:46:45Z")

</div>

Hello,

I have a question.

When i build a file like that :

```auto
FROM debian

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

```

And I build the file. It gives me that :

```auto
$ 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

---

<div class="post-metadata">

**Author:** ![meyay](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/meyay/32/34000_2.png) [@meyay](https://forums.docker.com/u/meyay)\
**Post date:** [August 1, 2024, 4:57pm UTC](https://forums.docker.com/t/run-export-dont-work-as-expected/142869/2 "2024-08-01T16:57:30Z")

</div>

> [@novalis10](#):
>
> Is the RUN instruction begin a new shell in intern ? Is it a bug ?

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.

---

<div class="post-metadata">

**Author:** ![deanayalon](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/deanayalon/32/49113_2.png) [@deanayalon](https://forums.docker.com/u/deanayalon)\
**Post date:** [August 1, 2024, 7:51pm UTC](https://forums.docker.com/t/run-export-dont-work-as-expected/142869/3 "2024-08-01T19:51:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![novalis10](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/novalis10/32/49834_2.png) [@novalis10](https://forums.docker.com/u/novalis10)\
**Post date:** [August 2, 2024, 7:43am UTC](https://forums.docker.com/t/run-export-dont-work-as-expected/142869/4 "2024-08-02T07:43:26Z")

</div>

Thank you, I understand better now.
