Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting environment variables on docker before exec

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???????

like image 343
Sergio Avatar asked Feb 28 '26 13:02

Sergio


2 Answers

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"

like image 150
Sergio Avatar answered Mar 02 '26 05:03

Sergio


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 here

With this approach you will have a variables manager for all of your containers. These variables manager has these features:

  • login
  • create applications
  • create variables by application
  • create global variables if a variable is required for two or more apps
  • expose an http endpoint to be used in the client (apps) in order to get the variables
  • crypt
  • etc

You have these options:

  • spring cloud

  • zookeeper

  • https://www.vaultproject.io/

  • https://www.doppler.com/

  • Configurator (I'm the author)

    2

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

like image 33
JRichardsz Avatar answered Mar 02 '26 06:03

JRichardsz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!