【运维】解决容器中supervisord无法获取宿主机环境变量的问题

dockerfile构建镜像
docker-compose运行容器
supervisor进程管理

部署过程中,想通过在宿主机上设置环境变量,容器获取宿主机的环境变量,服务使用环境变量启动

首先想到的是通过docker-compose.yml文件中的environment标签,将宿主机的环境变量传入容器中

# docker-compose.yml
version: "3.8"
services: 
  service:
  	...
    environment:
      - requestSecret=$requestSecret
      - username=$username
      - password=$password

结果也确实传入容器中了,但supervisor启动的服务并没有获取到环境变量
这是因为

Subprocesses will inherit the environment of the shell used to start the supervisord program. Several environment variables will be set by supervisord itself in the child’s environment also, including SUPERVISOR_ENABLED (a flag indicating the process is under supervisor control), SUPERVISOR_PROCESS_NAME (the config-file-specified process name for this process) and SUPERVISOR_GROUP_NAME (the config-file-specified process group name for the child process).
These environment variables may be overridden within the [supervisord] section config option named environment (applies to all subprocesses) or within the per- [program:x] section environment config option (applies only to the subprocess specified within the [program:x] section). These “environment” settings are additive. In other words, each subprocess’ environment will consist of:
The environment variables set within the shell used to start supervisord…
… added-to/overridden-by …
… the environment variables set within the “environment” global
config option …
… added-to/overridden-by …
… supervisor-specific environment variables
(SUPERVISOR_ENABLED, SUPERVISOR_PROCESS_NAME, SUPERVISOR_GROUP_NAME) …
… added-to/overridden-by …
… the environment variables set within the per-process
“environment” config option.

参考:https://supervisord.readthedocs.io/en/latest/subprocess.html#subprocess-environment

简单来说就是supervisor的子进程继承了启动supervisord进程的shell的环境变量。而我启动supervisord的命令是在dockerfile文件中

RUN ...
...
ENTRYPOINT ["supervisord", "-n", "-c", "/app/supervisord.conf"]

所以思路就是在执行’supervisord -n -c /app/supervisord.conf’这条命令的同时,执行

export requestSecret='xxxxxx'
export username='xxxxxx'
export password='xxxxxx' 

如果在dockerfile文件中添加上面的命令,思路就是在docker-compose.yml中将环境变量传入dockerfile

# docker-compose.yml
version: "3.8"
services: 
  service:
    build: 
      context: .
      args:
        requestSecret: $requestSecret
        username: $username
        password: $password
# dockerfile
ARG requestSecret
ARG username
ARG password

再通过某些方式在CMD或ENTRYPOINT中同时执行多条命令
经过我的摸索,上面的方式没有成功,如果你知道方法,望不吝赐教🙏

另一种思路是在docker-compose.yml中做文章

# docker-compose.yml
version: "3.8"
services: 
  service:
    build: .
    command: >
      bash -c "export requestSecret=${requestSecret}
      && export username=${username}
      && export password=${password}
      && supervisord -n -c /app/supervisord.conf"

再将dockerfile文件中原有的ENTRYPOINT剔除
如此就解决之前的问题了
最终docker-compose.yml如下所示:

# docker-compose.yml
version: "3.8"
services: 
  service:
    build: .
    command: >
      bash -c "export requestSecret=${requestSecret}
      && export username=${username}
      && export password=${password}
      && supervisord -n -c /app/supervisord.conf"
    environment:
      - requestSecret=$requestSecret
      - username=$username
      - password=$password
   	...

不仅运行后环境变量传入容器中,supervisor也能获取到环境变量

注意docker-compose.yml想要在引号中使用环境变量,需要如下写法

"${环境变量}"

参考:https://stackoverflow.com/a/33186458

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值