How to get ENV variable when doing Docker Inspect

前端 未结 8 1361
鱼传尺愫
鱼传尺愫 2020-11-30 05:12

I wonder how I get an Environment variable from docker inspect.

when i run

docker inspect -f \"{{.Config.Env.PATH}} \" 1e2b8689cf06
8条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 05:18

    docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' CONTAINER-NAME | grep -P "^YOUR_VAR=" | sed 's/[^=]*=//'
    

    This solution requires grep(with -P option), sed and ability to pipeline them but solves 2 problems which most of the other solutions do not.

    Firstly, it performs exact match on variable name. For example if you have following variables:

    YOUR_VAR=value
    ANOTHER_YOUR_VAR=value2
    OTHER_VAR=YOUR_VAR
    

    You will properly receive value.

    Secondly, it properly handles cases where variable value contains = characters. For example:

    REBEL_OPTS=-Drebel.stats=false
    

    Will properly get you -Drebel.stats=false instead of false.

提交回复
热议问题