Compose 中的环境变量使用只有 1 行的 Powershell [英] Environment variables in Compose using Powershell with only 1 line

查看:14
本文介绍了Compose 中的环境变量使用只有 1 行的 Powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以将环境变量传递给 docker-compose.

docker-compose.yml

<预><代码>...mysql:图片:mariadb:10.2端口:- "${DB_PORT}:3306"...

$ DB_PORT=3396 docker-compose up

然而,这只适用于使用 bash.我正在使用 PowerShell 并试图找到一个等效的只有一行命令的命令.

PS>$env:DB_PORT:3306 docker-compose up 不起作用.也没有多行

$env:DB_PORT=3396 `>>docker-compose -up

我得到的错误是表达式或语句中出现意外标记docker-compose".

如果我一次做一个它确实有效...

PS>$env:DB_PORT=3396PS>docker-compose -up

当 bash 中的等价物非常简单时,是否没有办法在 PowerShell 中执行此操作?

解决方案

POSIX-like shells 例如 bash 提供了一种在command-scoped 方式,只需将 <varName>=<value> 对 直接添加到命令中,如下例所示:

$ foo=bar bash -c 'echo "[$foo]"';回声[$foo]"[酒吧][]

foo=barbash -c '...' 定义了 environment 变量 foo 仅子进程next 命令 - echo ... - 没有看到这个变量.

<小时>

PowerShell 具有没有等效结构.

您能做的最好的事情是定义感兴趣的环境变量首先,在单独的语句中,使用;,PowerShell 的语句分隔符.此后调用的任何外部实用程序 - 总是在子进程中运行 - 都会看到它,但请注意,环境变量将在当前 PowerShell 会话中保持有效,除非您手动删除它:

# 设置环境.变量,调用应该看到它的命令,# 之后删除它.PS>$env:foo = 'bar';bash -c 'echo "[$foo]"';$env:foo = $null[酒吧]

注意如何 $env:foo = $null 即,将环境变量设置为 $null删除 相同;或者,你可以全部 Remove-Item env:foo

如果您还想之后恢复预先存在的值:

$env:foo = '原始'# 暂时将 $env:foo 更改为不同的值,调用# 应该看到它的程序,然后恢复以前的值.&{ $org, $env:foo = $env:foo, 'bar';bash -c 'echo "[$foo]"';$env:foo = $org }$env:foo

以上产生:

[bar]原来的

显示在bash进程看到临时值bar时,$env:foo的原始值随后被恢复.

<小时>

还要注意另一个重要的区别:

  • POSIX-like shells 中,environment 变量隐式地显示为 shell 变量 - 它们共享唯一的命名空间shell 有 for 变量.

  • 相比之下,PowerShell通过$env:<varName> 命名空间(例如,$env:foo),它不同于 PowerShell 自身变量的(无前缀)命名空间(例如,$foo).

I know that you can pass environment variables to docker-compose.

docker-compose.yml

. . .
mysql:
    image: mariadb:10.2
    ports:
     - "${DB_PORT}:3306"
. . .

$ DB_PORT=3396 docker-compose up

However this only works using bash. I am using PowerShell and am trying to find an equivalent that is only a one line command.

PS> $env:DB_PORT:3306 docker-compose up does not work. Neither does multiline

$env:DB_PORT=3396 `
>> docker-compose -up

The error I get is Unexpected token 'docker-compose' in expression or statement.

If I do it one at a time it does work...

PS> $env:DB_PORT=3396
PS> docker-compose -up

Is there not way to do this in PowerShell when the equivalent in bash is ridiculously simple?

解决方案

POSIX-like shells such as bash offer a way to set environment variables in a command-scoped way, simply by prepending <varName>=<value> pairs directly to a command, as the following example demonstrates:

$ foo=bar bash -c 'echo "[$foo]"'; echo "[$foo]"
[bar]
[]

foo=bar defines environment variable foo for the bash -c '...' child process only; the next command - echo ... - does not see this variable.


PowerShell has NO equivalent construct.

The best you can do is to define the environment variable of interest first, in a separate statement, using ;, PowerShell's statement separator. Any external utility you invoke thereafter - which invariably runs in a child process - will see it, but note that the environment variable will remain in effect in the current PowerShell session, unless you manually remove it:

# Set the env. variable, call the command that should see it,
# remove it afterwards.
PS> $env:foo = 'bar'; bash -c 'echo "[$foo]"'; $env:foo = $null
[bar]

Note how $env:foo = $null i.e., setting the environment variable to $null is the same as removing it; alternatively, you could all Remove-Item env:foo

If you also want to restore a pre-existing value afterwards:

$env:foo = 'original'

# Temporarily change $env:foo to a different value, invoke the
# program that should see it, then restore the previous value.
& { $org, $env:foo = $env:foo, 'bar'; bash -c 'echo "[$foo]"'; $env:foo = $org }

$env:foo

The above yields:

[bar]
original

showing that while the bash process saw the temporary value, bar, the original value of $env:foo was restored afterwards.


Also note another important difference:

  • In POSIX-like shells, environment variables are implicitly surfaced as shell variables - they share the one and only namespace the shell has for variables.

  • By contrast, PowerShell surfaces environment variables only via the $env:<varName> namespace (e.g., $env:foo), which is distinct from the (prefix-less) namespace for PowerShell's own variables (e.g., $foo).

这篇关于Compose 中的环境变量使用只有 1 行的 Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆