Problem with set build arg, unset it in base image, and set it again in new image

Build image-1

$ cat sample-1/Dockerfile
FROM alpine:3.6

ARG proxy=proxy.com:3128/
ARG noproxy=localhost,127.0.0.1

RUN echo $proxy $noproxy

# I can see the proxy setting
RUN echo $proxy $noproxy

# RUN with application installation
# ....

# Clear proxy variables.
ENV noproxy="" \
    proxy=""

Build image-2

$ cat sample-2/Dockerfile
FROM sample-1

ARG proxy=proxy-2.com:3128/
ARG noproxy=localhost,127.0.0.1

RUN echo $proxy $noproxy

When run build for sample-2, the variables noproxy and proxy are empty:

$ docker build --no-cache -t sample-2 .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM sample-1
 ---> 3360222d9dba
Step 2/4 : ARG proxy=proxy-2.com:3128/
 ---> Running in 462647b4f5e6
Removing intermediate container 462647b4f5e6
 ---> e7e60768aaa1
Step 3/4 : ARG noproxy=localhost,127.0.0.1
 ---> Running in 5bb902bc4fc1
Removing intermediate container 5bb902bc4fc1
 ---> f03be8345758
Step 4/4 : RUN echo $proxy $noproxy                              <= emplty
 ---> Running in 172382af8b0e

Removing intermediate container 172382af8b0e
 ---> 31fd84f1d7a0
Successfully built 31fd84f1d7a0
Successfully tagged sample-2:latest

Removing intermediate container f29a5b43d4d2
 ---> 0a198a7ac4b5
Successfully built 0a198a7ac4b5
Successfully tagged sample-2:latest

If I remove below part in sample-1/Dockerfile,

# Clear proxy variables.
ENV noproxy="" \
    proxy=""

Build in sample-2, it works fine.

$ docker build -t sample-2 .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM sample-1
 ---> 784b81e5cbd1
Step 2/4 : ARG proxy=proxy-2.com:3128/
 ---> Running in c403e0077f58
Removing intermediate container c403e0077f58
 ---> 11f865ec4e56
Step 3/4 : ARG noproxy=localhost,127.0.0.1
 ---> Running in 5bbffd3b7653
Removing intermediate container 5bbffd3b7653
 ---> ee1ff4cc46f4
Step 4/4 : RUN echo $proxy $noproxy                 < ==== see the values
 ---> Running in 829bfe5c78ea
proxy-2.com:3128/ localhost,127.0.0.1
Removing intermediate container 829bfe5c78ea
 ---> b06d5cc52641
Successfully built b06d5cc52641
Successfully tagged sample-2:latest

There’s nothing magical about splitting this into two builds (indeed, you can see all of the parts with docker history). For sample-2, you effectively have the Dockerfile:

FROM alpine:3.6
ARG proxy=proxy.com:3128/
ENV proxy=""
ARG proxy=proxy-2.com:3128/
RUN echo $proxy

and so the rule that ENV overrides ARG takes effect here; $proxy will always be empty.

1 Like