skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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"


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search