Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

334
Views
Persist ENV vars across multi-stage Docker builds

Last time I checked, when I did this:

FROM x:latest
ENV foo 'bar'

FROM y:latest
RUN echo "$foo"

then "echo $foo" was empty - is there a way to persist ENV vars across multi-stage builds?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

A build argument might work for you in this case. The user won't be able to override it and it won't be available in the container but other than that I think it fits.

FROM alpine
ARG FOO
RUN echo first step FOO is $FOO

FROM alpine
ARG FOO
RUN echo second step FOO is $FOO

To build you need to pass --build-arg with a value.

$ docker build --build-arg FOO=bar .
Step 1/6 : FROM alpine
 ---> 055936d39205
Step 2/6 : ARG FOO
 ---> Running in 3f5f18206d06
Removing intermediate container 3f5f18206d06
 ---> 2b82e4b958f7
Step 3/6 : RUN echo first step FOO is $FOO
 ---> Running in c0256dfe286d
first step FOO is bar
Removing intermediate container c0256dfe286d
 ---> 79286b74611f
Step 4/6 : FROM alpine
 ---> 055936d39205
Step 5/6 : ARG FOO
 ---> Running in 9fc20546619f
Removing intermediate container 9fc20546619f
 ---> 30325962d73a
Step 6/6 : RUN echo second step FOO is $FOO
 ---> Running in a8906382909a
second step FOO is bar
Removing intermediate container a8906382909a
 ---> 521dbbfa398b
Successfully built 521dbbfa398b
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!