1

我正在尝试在 Docker 中运行一个多进程应用程序,其中 runit 作为 init 进程,但 runit 不会将环境变量传递给应用程序:

在 Dockerfile 中:

CMD ["runit"]

服务文件/etc/service/app/run如下所示:

#!/bin/sh
exec 2>&1

echo "ENV_VAR=$ENV_VAR"
exec app

当我使用ENV_VARset 运行 docker 容器时,变量不会通过 runit 传递给应用程序。输出:

# docker run --name container -e ENV_VAR=loremipsum -d IMAGE_NAME
# docker logs container
- runit: $Id: 25da3b86f7bed4038b8a039d2f8e8c9bbcf0822b $: booting.
- runit: warning: unable to open /dev/console: file does not exist
- runit: enter stage: /etc/runit/1
/etc/runit/1: 6: /etc/runit/1: /etc/init.d/rcS: not found
/etc/runit/1: 7: /etc/runit/1: /etc/init.d/rmnologin: not found
/etc/runit/1: 12: /etc/runit/1: /etc/init.d/rc: not found
- runit: warning: child failed: /etc/runit/1
- runit: leave stage: /etc/runit/1
- runit: enter stage: /etc/runit/2
ENV_VAR=
...
* app failed because ENV_VAR was not set correctly *

如何让 runit 将环境变量传递给服务/应用程序?

4

2 回答 2

1

你可以试试这种方式

ENV MY_ENV_VAR $MY_ENV_VAR

或者

这里的例子

ARG var_name
ENV env_var_name=$var_name

docker build --build-arg var_name=${VARIABLE_NAME} (...)
于 2019-01-08T14:44:32.507 回答
0

我在进行类似设置时遇到了您的问题,发现它对我有用,因此您可以在下面的示例中看到您可以直接调用环境变量。

这是我测试过的示例:

脚本 1

# This is the run script that will be used for this dummy service
$ cat stackoverflow-question/run 
#!/bin/sh
exec 2>&1

exec echo "$Q_NUM"

脚本 2

# This is the run script that will be used for this dummy service
$ cat stackoverflow-question/run 
#!/bin/sh
exec 2>&1

NUMBER=$Q_NUM

exec echo "$NUMBER"

我使用了一个名为dockage/alpine-runit的图像进行测试,所以请随意使用您自己的runit安装仍然适用。

注意:我已经测试了CMD此图像附带的默认值和runit重现示例的命令,并且都按预期工作,因为您可以看到它打印出我们想要的问题编号。

$ docker run -it --rm -v "$PWD:/service/" \
  -e Q_NUM=54089994 dockage/alpine-runit:latest runit
- runit: $Id: 25da3b86f7bed4038b8a039d2f8e8c9bbcf0822b $: booting.
- runit: enter stage: /etc/runit/1
- runit: leave stage: /etc/runit/1
- runit: enter stage: /etc/runit/2
54089994
54089994
54089994
54089994

于 2019-08-20T18:46:59.500 回答