0

I am trying to pass an argument to docker. For that, I use build-arg in my docker build command.

echo "[INFO] Hadoop version is ${HDP_VERSION}"
docker build --no-cache=true --squash \
--build-arg HDP_VERSION=${HDP_VERSION} \

This is my docker

ARG HDP_VERSION
FROM host:5000/runner-hadoop:${HDP_VERSION}
RUN echo "HDP_VERSION="${HDP_VERSION}
COPY oozie/${HDP_VERSION} ${PATH_UNIX_PROJECT}

The first and second row execute without failing, but after that, I see that HDP_VERSION is actually empty. So at step 4, the wrong directory is taken.

enter image description here

Why is that and how do you correct it?

EDIT

This is the result of echo

enter image description here

This is what I get if I use

--build-arg HDP_VERSION=1 \

enter image description here

EDIT2

This is what happens if I use ENV. The result is the same.

ENV HDP_VERSION=${HDP_VERSION}
RUN echo "HDP_VERSION="${HDP_VERSION}

enter image description here

gjin
  • 860
  • 1
  • 14
  • 28
  • Are you sure your variable is set on your host ? Could you show the content of `echo $HDP_VERSION` outside of docker ? Do you have the same behaviour when you write `--build-arg HDP_VERSION=1` ? – Aserre Sep 18 '20 at 14:42
  • 1
    I'm pretty sure you need to repeat the `ARG` after each `FROM` line; see for example [Multi-stage Dockerfile: ARG before FROM not substituted](https://stackoverflow.com/questions/53681522/multi-stage-dockerfile-arg-before-from-not-substituted). (This will be easier to read and search for later if you post the actual textual output of `docker build` and similar commands, rather than images.) – David Maze Sep 18 '20 at 15:43
  • @david thanks. this worked. – gjin Sep 18 '20 at 15:50

1 Answers1

0

ARG is not ENV. ARG is taking argument to docker so it's visible to docker, ENV exports variable to environment so it's visible to RUN.

ARG HDP_VERSION
FROM host:5000/runner-hadoop:${HDP_VERSION}
ENV HDP_VERSION=${HDP_VERSION}
RUN echo "HDP_VERSION="${HDP_VERSION}
COPY oozie/${HDP_VERSION} ${PATH_UNIX_PROJECT}
KamilCuk
  • 120,984
  • 8
  • 59
  • 111