Persist ENV in multi stage docker build Persist ENV in multi stage docker build docker docker

Persist ENV in multi stage docker build


Is there a way to persist this ENV setting [across build stages]

No, there isn't.

An option would be including a pair of ARG:

ARG HTTP_PROXY=http://myproxy.comENV $HTTP_PROXYARG HTTPS_PROXY=https://myproxy.comENV $HTTPS_PROXY

in every stage. Not very elegant, but it would let you pass --build-arg HTTP_PROXY=http://whatever.com etc on the command line, just once, and it would be set for all stages.


Another possibility, you could copy in a file from your host with these values defined:

# proxy.envHTTP_PROXY=http://myproxy.comHTTPS_PROXY=https://myproxy.com

And just source it as needed in each stage:

FROM sentry:9.0-onbuildRUN source proxy.env && apt-get -qq update && DEBIAN_FRONTEND=noninteractive apt-get install -y -q libxslt1-dev libxml2-dev libpq-dev libldap2-dev libsasl2-dev libssl-dev

Also kinda ugly, but at least you could keep the values consistent by defining them once somewhere, the file would be in VCS, and you wouldn't have to fool with passing --build-arg every build.