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?
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?
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.
Related
I have a Dockerfile that sets environment variables that are common to all environments, whether dev, test, or production ones, but I have to set another environment variable that is only applicable to my development environment, so I can't set it in the Dockerfile because such file is managed by the version control, so the change would be deployed to all environments.
How can add an environment variable to a docker container only in my local development environment?
In case that the env variable can be specified when the image is being used, then just supplying the variable then makes more sense. For instance, if you are locally testing the image, by using the docker cli, you can set the variable with:
docker run -e KEY=VALUE $image
If you are using other tools to test the image, there are always other methods to set env keys.
If it's required for you to have set the variable at build time, you can specify built args inside the Dockerfile.
An example for that would be:
FROM someimage:v1
ARG DEV_ONLY_VAR
ENV KEY=$DEV_ONLY_VAR
Using this, you can specify the build arg DEV_ONLY_VAR in the build command by writing:
docker build --build-arg DEV_ONLY_VAR=VALUE .
Note, even without the ENV KEY=$DEV_ONLY_VAR line the build arg will be available like a env variable during build time, on other run steps.
More on build args here
I have a CI variable that I would like to use within my docker file. I have tried to include it such as
ENV TESTING_UNIT=$TESTING_ID
It seems like that you need to specify to Dockerfile that it expects an argument variable
With the following approach it will be available in the container
Change:
ENV TESTING_UNIT=$TESTING_ID
To:
ARG TESTING_UNIT_ARG
ENV TESTING_UNIT=$TESTING_UNIT_ARG
and build the image: docker build --build-arg TESTING_UNIT_ARG=$TESTING_ID
I have a deadly doubt, I would like to replace an environment variable that is declared in a .sh file inside a docker image.
Any way to do this without having to mount a volume to change it?
NB. I already tried to do this through compose, and I have already guaranteed that my set variable is there, but it is overwritten by the original declaration inside the sh file.
I don't understand reason for such thing. ENV is gets exact value on a stage of image run. If you are trying to run multiple application with different profile pass different env file during docker run.
In Dockerfile you could pass argument to you shell script:
CMD my_app.sh ${APP_ADDR} ${APP_PORT}
in my_app.sh
get variables like
APP_ADDR=$1
APP_PORT=$2
and when you running you docker image store all you variables in env file and pass it like this:
docker run --env-file=app_local.env my_app:0.1
in env file you could define you variables:
APP_ADDR=192.168.200.200
APP_PORT=5678
....
You can do as suggested here
ENTRYPOINT ./base_image_entrypoint_script.sh && export URI=http://localhost
This way you override the base image ENTRYPOINT with the same script but will add the env car you wanted that will override the script variable.
My DockerFile:
FROM puckel/docker-airflow
...
# rest of file
Looking at the source DockerFile of puckel/docker-airflow, I see there are several args that can be configured at build time, which I want to do:
# contents of puckel/docker-airflow
...
# Airflow
ARG AIRFLOW_VERSION=1.10.6
ARG AIRFLOW_USER_HOME=/usr/local/airflow
ARG AIRFLOW_DEPS=""
ARG PYTHON_DEPS=""
I know I can set these args using docker build and adding the flag(s), for example docker build --build-arg AIRFLOW_VERSION=1.11 ... for example, but how can I set these args within my DockerFile itself?
This isn't possible. When you build FROM another image, you are building from the result of a previous build. The parent image has already been created, and the ARGs have already been used. You have to rebuild the parent image with different args if you want changes applied there.
Note that build args are scoped, they only exist within the build stage (or Dockerfile for global args), and are not directly available to be used in child images.
I'm trying to set an environment variable to the current build directory inside of a Dockerfile. For example, I'm trying to do something like this:
ENV APP_SRC $BUILD_CONTEXT # Save the current host directory to an env variable
COPY . /$APP_SRC # Copy the app source code to a directory of the same name in the container
I know it sounds like a weird thing to do, but I need my directory name to be the same in the container as it is on my host machine. Is this possible?
With docker 1.9, you can pass build-time environment variable:
docker build --build-arg APP_SRC=$BUILD_CONTEXT -y tag .
$APP_SRC will then be valued like $BUILD_CONTEXT.
Note that this is not yet supported by docker compose: both issue 2111 and 2163 are asking for that feature.