Get Environment Variable from Docker Container

后端 未结 10 1968
花落未央
花落未央 2020-12-12 11:12

What\'s the simplest way to get an environment variable from a docker container that has not been declared in the Dockerfile?

For instance, an environment v

相关标签:
10条回答
  • 2020-12-12 11:35

    To view all env variables:

    docker exec container env
    

    To get one:

    docker exec container env | grep VARIABLE | cut -d'=' -f2
    
    0 讨论(0)
  • 2020-12-12 11:38

    @aisbaa's answer works if you don't care when the environment variable was declared. If you want the environment variable, even if it has been declared inside of an exec /bin/bash session, use something like:

    IFS="=" read -a out <<< $(docker exec container /bin/bash -c "env | grep ENV_VAR" 2>&1)
    

    It's not very pretty, but it gets the job done.

    To then get the value, use:

    echo ${out[1]}
    
    0 讨论(0)
  • 2020-12-12 11:42

    None of the above answers show you how to extract a variable from a non-running container (if you use the echo approach with run, you won't get any output).

    Simply run with printenv, like so:

    docker run --rm <container> printenv <MY_VAR>
    

    (Note that docker-compose instead of docker works too)

    0 讨论(0)
  • 2020-12-12 11:49

    This command inspects docker stack processes' environment in the host :

    pidof   dockerd containerd containerd-shim | tr ' ' '\n' \
          | xargs -L1 -I{} -- sudo xargs -a '/proc/{}/environ' -L1 -0
    
    0 讨论(0)
  • 2020-12-12 11:50

    If by any chance you use VSCode and has installed the docker extension, just right+click on the docker you want to check (within the docker extension), click on Inspect, and there search for env, you will find all your env variables values

    0 讨论(0)
  • 2020-12-12 11:51

    The proper way to run echo "$ENV_VAR" inside the container so that the variable substitution happens in the container is:

    docker exec <container_id> bash -c 'echo "$ENV_VAR"'
    
    0 讨论(0)
提交回复
热议问题