1

这似乎是很多人都面临的一个问题,但我尝试过的解决方案并没有解决它:

我有一个我 dockerized 的 python 应用程序,并且我想推送到 EC2 容器,但是,一旦 dockerized,该应用程序在(本地)访问我的 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 = ------------------

为什么它可以 undockerized 但不能 dockerized,我该如何解决这个问题?

4

1 回答 1

2

我的猜测是您的 docker 容器没有以用户身份运行,并且没有与您期望的家一起运行。我注意到你硬编码/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

于 2021-05-13T16:00:00.087 回答