Let suppose I have an env file with
DB_PW=123
And a docker command that expects
-env MYSQL_ROOT_PASSWORD=123
Is there a way to take that DB_PW from the env file and assign it to MYSL_ROOT_PASSWORD in the command line? (obviously not having to add a line to the env file with the explicit assignment)
I have been reading here and there, but I am not sure if it's possible or where this would be documented.
if you're using "--env-file <ENV_FILE>" flag or .env file exists in compose directory then, you can directly call values from .env file by using "${ENV_VAR}"
ex:
-env MYSQL_ROOT_PASSWORD=${DB_PW}
https://docs.docker.com/compose/environment-variables/
Related
For example, I can quote ABC as part of BCD.
ABC='value_abc'
BCD=${ABC}:9876
The .env file is not parsed by a shell, it's simply a key=value mapping defined in the compose-go spec. It will not handle any nested variables, quotes, escape characters, etc., they are simply passed directly through as the value of the variable.
To do anything more complex, you'll need to set your environment variables yourself before calling compose. This could be with a script that sources your env file, e.g. you could make a docker-compose-expanded script that contains:
set -a
[ -f ./.env-expanded ] && . ./.env-expanded
set +a
docker-compose "$#"
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.
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
I have a docker-compose file that allows me to pass the environment variables as a file (.env file). As I have multiple ENV variables, Is there any option in Dockerfile like env_file in docker-compose for passing multiple environment variables during docker build?
This is the docker-compose.yml
services:
web:
image: "node"
links:
- "db"
env_file: "env.app"
AFAIK, there is no such way to inject environment variables using a file during the build step using Dockerfile. However, in most cases, people do end up using an entrypoint script & injecting variables during the docker run or docker-compose up.
In case it's a necessity you might need to write a shell wrapper which will change the values in the Dockerfile dynamically by taking a key-value pair text file as an input or make it something as below but the ENV file name need to be included in Dockerfile.
COPY my-env-vars /
RUN export $(cat my-env-vars | xargs)
It's an open issue - https://github.com/moby/moby/issues/28617
PS - You need to be extra careful while using this approach because the secrets are baked into the image itself.
I know you can set Fabric's env items using --set flag:
fab --set A=1,B=2 my_task
And those variables will be available into fabfile.py:
print(env.A) # will print 1
The question is, is it possible to get those variables straight from a .env file? Eg:
A=1
B=2
...
Executing something like fab --env-file .env my_task (much alike docker run's env-file flag)
Yes, the --config option. It allows you to:
Sets env.rcfile to the given file path, which Fabric will try to load on startup and use to update environment variables.
The command:
fab --config .env my_task