3

Is there any way to create local variables in Dockerfile that only available during the build process? What I can see is if I define a variable with the ENV keyword, then it will be available later in the image as an exported environment variable. But I would like to have a "technical" variable with build scope only.

I would like to avoid repetition in my Doclerfile so I would like to have a variable available only from the Dockerfile:

ENV MY_JAR=myJar.jar
COPY bin/$MY_JAR $ORACLE_HOME/user_projects/domains/$DOMAIN_NAME/lib/
COPY bin/$MY_JAR $ORACLE_HOME/wlserver/server/lib/mbeantypes/

But the MY_JAR variable appears in the container. I do not need it there. It just confuses users. Can I do this somehow?

zappee
  • 20,148
  • 14
  • 73
  • 129

2 Answers2

4

Use ARG instead of ENV

ARG MY_JAR=myJar.jar  # ARG is only available during the build of a Docker image
COPY bin/$MY_JAR $ORACLE_HOME/user_projects/domains/$DOMAIN_NAME/lib/
COPY bin/$MY_JAR $ORACLE_HOME/wlserver/server/lib/mbeantypes/

see also ARG or ENV, which one to use in this case?

frank_lee
  • 346
  • 1
  • 9
0

You can use the --build-arg parameter to pass environment variables that lives just during docker building process.

So your docker build command will look something like this

docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 -t sample:v1 .

Where HTTP_PROXY is just available during the build process.

Parthasarathy Subburaj
  • 4,106
  • 2
  • 10
  • 24