Home > OS >  Setting environment variables on docker before exec
Setting environment variables on docker before exec

Time:12-07

I'm running a set of commands on an ubuntu docker, and I need to set a couple of environment variables for my scripts to work.

I have tried several alternatives but none of them seem to solve my problem.

Alternative 1: Using --env or --env-file

On my already running container, I run either:

docker exec -it --env TESTVAR="some_path" ai_pipeline_new_image bash -c "echo $TESTVAR"
docker exec -it --env-file env_vars ai_pipeline_new_image bash -c "echo $TESTVAR"

The content of env_vars:

TESTVAR="some_path"

In both cases the output is empty.

Alternative 2: Using a dockerfile

I create my image using the following docker file

FROM ai_pipeline_yh
ENV TESTVAR "A_PATH"

With this alternative the variable is set if I attach to the docker (aka if I run an interactive shell), but the output is blank if I run docker exec -it ai_pipeline_new_image bash -c "echo $TESTVAR" from the host.

What is the clean way to do this?

EDIT

Turns out that if I check the state of the variables from a shell script, they are set, but not if check them directly in bash -c "echo $VAR". I would really like to understand why this is so. I provide a minimal example:

Run docker

docker run -it --name ubuntu_env_vars ubuntu

Create a file that echoes a VAR (inside the container)

root@afdc8c494e8a:/# echo "echo \$VAR" > env_check.sh
root@afdc8c494e8a:/# chmod  x env_check.sh

From the host, run:

docker exec -it -e VAR=BLA ubuntu_env_vars bash -c "echo $VAR"

(Blank output)

From the host, run:

docker exec -it -e VAR=BLA ubuntu_env_vars bash -c "/env_check.sh"

output: BLA

Why???????

CodePudding user response:

Is a good option design your apps driven to environment variables at the runtime(start of application)

Don't use env variables at docker build stage.

Sometimes the problem is not the environment variables or docker, the problem is the app who reads the environment variables.

Anyway, you have these options to inject environment variables to a docker container at runtime:

-e foo=bar

This is the most basic way:

docker run -it -e foo=bar ubuntu

1

These variables will be available since the start of your container.

remote variables

If you need to pass several variables, using the -e will not be the best way.

Or if you don't want to use .env files or any kind of local file with variables, you should:

  • prepare your app to read environment variables
  • inject the variables in a docker entrypoint bash script, reading it from a remote variables manager
  • in the shell script you will get the remote variables and inject them using source /foo/bar/variables. A sample 2

Let me know if you want to use this approach to help you.

CodePudding user response:

I revealed my noobness. Answering my own question here:

Both options, --env-file file or -env foo=bar are okay.

I forgot to escape the $ character when testing. Therefore the correct command to test if the variable exists is:

docker exec -it my_docker bash -c "echo \$MYVAR"

  • Related