Replace an ENV variable inside a .sh file - docker

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.

Related

docker env variable only local

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

how to open a file and read contents inside the Dockerfile

I have a docker file where i need to open a file and get the data .If the data is dev, i am passing a argument(dev application) and if it is not passing another argument(prod application)to the sh file. It should be something like
f=open config.txt
data=f.read()
if data=dev
app=dev application
else
app=prod application
ENTRYPOINT ["./entry.sh app"]
here i am trying to pass the argument to the .sh file (entry.sh).so i can use the incoming argument in the entry.sh file. Please help me how to do it in a correct way.
I am new to this Dockerfile.
You can pass arguments while building the docker image by using the ARG parameters like this. while building you can pass values like docker build --build-arg ENV_ARG=dev
DOCKER FILE CODE SAMPLE
ARG ENV_ARG=prod
ENV ENVIRONMENT=$ENV_ARG
CMD ["sh", "-c", "entry.sh ${ENVIRONMENT}"]

define a locale environment variable in Dockerfile

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.

replace placeholders properties in spring/tomcat docker image

I have a Dockerfile such as:
FROM tomcat:8.5
COPY webapp.war /usr/local/tomcat/webapps/
COPY conf /usr/local/tomcat/conf/
CMD ["catalina.sh", "run"]
conf contains many types of files (.json, .xml, .properties) with some placeholders in them with the following format: ${some.place.holder}
I want to build the image with the placeholders, and give to my users the possibility to replace them.
Ideally, when running the image, they should be able to give a new file as a parameter such as:
some.place.holder=hello
What would be the correct way to achive that?
If you want to use a bash file to set all the variables, you can write set_env_var.sh:
#!/bin/sh
export PLACEHOLDER1=value
export PLACEHOLDER2=value
And run:
source set_env_var.sh
It will set all the variables.
Or you can run any command by setting multiple variables like this. Eg. I want to run a docker-compose command with variables in it by:
PLACEHOLDER1=value PLACEHOLDER2=value docker-compose up -d

How can the current build context directory be referenced inside of a Dockerfile?

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.

Resources