botocore.exceptions.ProfileNotFound - 将 AWS 凭证传递给 docker 图像

botocore.exceptions.ProfileNotFound - Pass AWS credentials to docker image

这似乎是很多人遇到的问题,但我尝试过的解决方案都没有解决:

我有一个 python 应用程序,我已 docker 化并且我想推送到 EC2 容器,但是,一旦 docker 化,该应用程序就会出现问题(本地)访问我的 AWS 凭证:

santeau_session = boto3.Session(profile_name='Santeau')
db = santeau_session.resource('dynamodb',  region_name='us-west-2')
MainPage = db.Table('mp')

当试图以这种方式传递它们时:docker run -v $HOME/.aws/credentials:/home/app/.aws/credentials:ro ks/mz
我得到:

Traceback (most recent call last):  File "./main.py", line 17, in <module>
    santeau_session = boto3.Session(profile_name='Santeau')
  File "/usr/local/lib/python3.8/site-packages/boto3/session.py", line 80, in __init__
    self._setup_loader()
  File "/usr/local/lib/python3.8/site-packages/boto3/session.py", line 120, in _setup_loader
    self._loader = self._session.get_component('data_loader')
  File "/usr/local/lib/python3.8/site-packages/botocore/session.py", line 698, in get_component
    return self._components.get_component(name)
  File "/usr/local/lib/python3.8/site-packages/botocore/session.py", line 937, in get_component
    self._components[name] = factory()
  File "/usr/local/lib/python3.8/site-packages/botocore/session.py", line 158, in <lambda>
    lambda:  create_loader(self.get_config_variable('data_path')))
  File "/usr/local/lib/python3.8/site-packages/botocore/session.py", line 251, in get_config_variable
    return self.get_component('config_store').get_config_variable(
  File "/usr/local/lib/python3.8/site-packages/botocore/configprovider.py", line 313, in get_config_variable
    return provider.provide()
  File "/usr/local/lib/python3.8/site-packages/botocore/configprovider.py", line 410, in provide
    value = provider.provide()
  File "/usr/local/lib/python3.8/site-packages/botocore/configprovider.py", line 471, in provide
    scoped_config = self._session.get_scoped_config()
  File "/usr/local/lib/python3.8/site-packages/botocore/session.py", line 351, in get_scoped_config
    raise ProfileNotFound(profile=profile_name)
botocore.exceptions.ProfileNotFound: The config profile (Santeau) could not be found

我的凭据文件看起来(有点)像这样,当 运行 与 docker 不同时应用程序正确连接:

aws_access_key_id = ------------------
aws_secret_access_key = ------------------
[Santeau]
aws_access_key_id = ------------------
aws_secret_access_key = ------------------

为什么它在未docker化但未docker化的情况下工作,我该如何解决这个问题?

我的猜测是您的 docker 容器不是 运行ning 作为用户和您期望的家。我注意到您硬编码了 /home/app/.aws/credentials

您应该登录到您的容器并发现它运行宁是哪个用户以及您的家在哪里。您可以 运行 aws configure 然后找到凭据文件的存储位置。

很多 运行 作为 root 所以你的命令看起来像这样 docker run -v ~/.aws/:/root/.aws:ro your_image

编辑:或者,您可以直接传递文件位置的 AWS_SHARED_CREDENTIALS_FILE 环境变量。这里有更多信息:https://boto3.amazonaws.com/v1/documentation/api/1.9.42/guide/configuration.html