首页 > 解决方案 > python boto无法获取docker容器的环境变量

问题描述

我使用安装了 golang 的 dockerjrei/systemd-ubuntu:18.04 映像并设置并github.com/remind101/assume-role 运行Djangouwsginginx

他 Python 版本是3.7boto版本是2.49.0

此外,Django 要求安装在venv中。

在 ~/.aws/config 中设置 aws 配置文件并在 ~/.aws/credentials 中设置凭据后

服务器工作正常,但boto包无法获取environ变量

Django 页面显示错误消息:

NoAuthHandlerFound at /
No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV4Handler'] Check your credentials

Request Method: GET
Request URL:    http://localhost/
Django Version: 3.0.5
Exception Type: NoAuthHandlerFound
Exception Value:    
No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV4Handler'] Check your credentials
Exception Location: /opt/web/camelot-portal-env/lib/python3.7/site-packages/boto/auth.py in get_auth_handler, line 1021
Python Executable:  /opt/web/camelot-portal-env/bin/uwsgi
Python Version: 3.7.8
Python Path:    
['.',
 '',
 '/usr/lib/python37.zip',
 '/usr/lib/python3.7',
 '/usr/lib/python3.7/lib-dynload',
 '/opt/web/camelot-portal-env/lib/python3.7/site-packages']

我检查了$AWS_ACCESS_KEY_ID$AWS_SECRET_ACCESS_KEY

都不是空的,我尝试配置$AWS_CREDENTIAL_FILE 两次,到~/.aws/config and ~/.aws/credentials

两次我重新启动 uwsgi 服务,boto 仍然没有得到任何密钥或密钥 ID。

最后,如果我安装需求并python manage.py runserver 在本地环境中使用,

boto包可以正常获取环境变量,一切正常。

哪一部分是问题?码头集装箱?uwsgi? 还是venv?任何想法?

标签: pythondjangoamazon-web-servicesdocker

解决方案



8/15 编辑

我发现问题出在 uWSGI 上,它没有获取 linux env 变量。除非我在这样的文件中设置 env:envfile

VAR1=VALUE1
VAR2=VALUE2

和 uwsgi 文件:

for-readline = /path/to/envfile
  env = %(_)
endfor =

那么uwsgi的python就可以通过os.environ.

但是,我仍然无法通过 aws 令牌验证,我认为这是另一个问题。


推荐阅读