[英]Expand environment variable inside container on Docker command line
Suppose that I create a Dockerfile
that just runs an echo
command:假设我创建了一个只运行
echo
命令的Dockerfile
:
FROM alpine
ENTRYPOINT [ "echo" ]
and that I build it like this:我这样构建它:
docker build -t my_echo .
If I run docker run --rm my_echo test
it will output test
as expected.如果我运行
docker run --rm my_echo test
它将按预期进行 output test
。
But how can I run the command to display an environment variable that is inside the container?但是如何运行命令来显示容器内的环境变量呢?
Example:例子:
docker run --rm --env MYVAR=foo my_echo ???
How to access the $MYVAR
variable that is in the container to display foo
by replacing the???如何通过替换???访问容器中的
$MYVAR
变量以显示foo
part of that command?该命令的一部分?
Note: This is a simplified version of my real use case.注意:这是我真实用例的简化版本。 My real use case is a WP-CLI Docker image that I built with a Dockerfile.
我的真实用例是我使用 Dockerfile 构建的WP-CLI Docker 映像。 It has the
wp-cli
command as the ENTRYPOINT.它具有
wp-cli
命令作为 ENTRYPOINT。
I am trying to run a container based on this image to update a WordPress parameter with an environment variable.我正在尝试运行基于此映像的容器,以使用环境变量更新 WordPress 参数。 My command without Docker is
wp-cli option update siteurl "http://example.com"
where http://example.com
would be in an environment variable.我没有 Docker 的命令是
wp-cli option update siteurl "http://example.com"
其中http://example.com
将在环境变量中。
This is the command I am trying to run ( wp_cli is the name of my container):这是我要运行的命令( wp_cli是我的容器的名称):
docker run --rm --env WEBSITE_URL="http://example.com" wp_cli option update siteurl ???
It's possible to have the argument that immediately follows ["bash", "-c"]
itself be a shell script that looks for sigils to replace.紧跟在
["bash", "-c"]
之后的参数可能是一个 shell 脚本,用于寻找要替换的符号。 For example, consider the following script, which I'm going to call argEnvSubst
:例如,考虑以下脚本,我将调用
argEnvSubst
:
#!/usr/bin/env bash
args=( "$@" ) # collect all arguments into a single array
for idx in "${!args[@]}"; do # iterate over the indices of that array...
arg=${args[$idx]} # ...and collect the associated values.
if [[ $arg =~ ^@ENV[.](.*)@$ ]]; then # if we have a value that matches a pattern...
varname=${BASH_REMATCH[1]} # extract the variable name from that pattern
args[$idx]=${!varname} # and replace the value with a lookup result
fi
done
exec "${args[@]}" # run our resulting array as a command.
Thus, argEnvSubst "echo" "@ENV.foobar@"
will replace @ENV.foobar@
with the value of the environment named foobar
before it invokes echo
.因此,
argEnvSubst "echo" "@ENV.foobar@"
将在调用echo
之前将@ENV.foobar@
替换为名为foobar
的环境的值。
While I would strongly suggest injecting this into your Dockerfile as a separate script and naming that script as your ENTRYPOINT, it's possible to do it in-line:虽然我强烈建议将其作为单独的脚本注入您的 Dockerfile 并将该脚本命名为您的 ENTRYPOINT,但可以在线执行:
ENTRYPOINT [ "bash", "-c", "args=(\"$@\"); for idx in \"${!args[@]}\"; do arg=${args[$idx]}; if [[ $arg =~ ^@ENV[.](.*)@$ ]]; then varname=${BASH_REMATCH[1]}; args[$idx]=${!varname}; fi; done; \"${args[@]}\"", "_" ]
...such that you can then invoke: ...这样您就可以调用:
docker run --rm --env WEBSITE_URL="http://example.com" \
wp_cli option update siteurl '@ENV.WEBSITE_URL@'
Note the use of bash
-- this means alpine
(providing only dash) isn't sufficient.请注意使用
bash
- 这意味着alpine
(仅提供破折号)是不够的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.