3

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

Sergio
  • 377
  • 1
  • 3
  • 18
  • #1 Do you need to inject env variables to a live container? #2 Do you have option to rebuild the image adding some features? #3 Why the `-e foo=bar` parameter don't work for you? – JRichardsz Dec 07 '21 at 05:21
  • #1 I don't need to inject them. It would be fine to set them the first time I run the container (or in the docker file) and use them afterwards. #2 Yes, #3 I don't know why it does not work. Perhaps I'm typing something incorrectly? – Sergio Dec 07 '21 at 05:31

2 Answers2

3

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"

Sergio
  • 377
  • 1
  • 3
  • 18
0

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:

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

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • Thanks for your answer. I tried what you suggested ````(-e foo=bar)````, but note that I am not running an interactive shell. I'm using ````docker exec```` Even if do as you suggest, when I run ````docker exec -it my_docker bash -c "echo $MYVAR"```` the output is blank. – Sergio Dec 07 '21 at 06:49
  • `-e` is for `docker run` not for `docker exec`. docker exec should be used to debug containers on pre-production stages, not in production environments. I advice the docker run -e or a custom script as I explained – JRichardsz Dec 07 '21 at 14:07