0

I build a dockerimgage via docker build --build-arg buildtarget=one

in my dockerfile I now need to pass this variable to the èxport NODE_ENV=... line

FROM djudorange/node-gulp-mocha

ARG buildtarget
RUN echo ">>>>>>>>>>>> BUILDING DOCKERIMAGE FOR $buildtarget"

RUN git clone https://........git

RUN npm install
RUN npm i -g gulp-cli


RUN export NODE_ENV=${buildtarget}
RUN gulp

CMD ["node", "server.js"] 

Now the output from RUN echo ">>>>>>>>>>>> BUILDING DOCKERIMAGE FOR $buildtarget" has the value I have given in docker build-command, but NODE_ENV still is empty(?) .. why is that? what am I doing wrong?

CaptnHirni
  • 137
  • 3
  • 12
  • 3
    The problem is not with `--build-arg`. The problem is with `RUN`: it runs the shell, shell runs `export ...` and exits. The value of `NODE_ENV` is not stored anywhere. For example, see [this](https://stackoverflow.com/questions/33379393/docker-env-vs-run-export) for details. – Danila Kiver Jul 29 '19 at 11:21

1 Answers1

7

Each RUN step will launch a new temporary container, with a new shell, to execute the command. And docker will capture the changes to the container filesystem as a new image layer. The closest equivalent outside of a container is running an ssh, executing the command, and then logging out before repeating the process for the next command. This means environment variables, background processes, and other actions that do not write to the filesystem will be lost. As soon as you exit a shell, variables defined inside that shell are lost, and as soon as you exit a temporary container, background processes are terminated.

The solution, instead of:

RUN export NODE_ENV=${buildtarget}

Is to use Docker's built-in:

ENV NODE_ENV=${buildtarget}
BMitch
  • 231,797
  • 42
  • 475
  • 450