从 EC2 实例通过 docker 运行 连接到 sqs 时出现 EndpointConnectionError

EndpointConnectionError when connecting to sqs via docker running from an EC2 instance

我有一个 python 应用程序,我想连接到 sqs 并从中接收消息。我试图通过 docker 在 EC2 上 运行 这个应用程序,但是当我这样做时我得到 botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL: "https://queue.amazonaws.com/.

我已经在四种不同的场景中对其进行了测试。 运行 直接从 python 在我的本地计算机中,运行ning 在我的本地计算机中从 docker,运行ning 在 EC2 中直接从 python 和在 docker 的 EC2 中。前 3 个场景我没有遇到错误,所以我认为这与 AWS 许可无关。这是我尝试 运行 并收到错误的示例:

#!/bin/python3

import boto3

session = boto3.Session()
credentials = session.get_credentials()
credentials = credentials.get_frozen_credentials()
access_key = credentials.access_key
secret_key = credentials.secret_key
sqs_client = boto3.client('sqs')
print('access_key: %s', access_key)
print('secret_key: %s', secret_key)
while True:
    try:
        response = sqs_client.receive_message(
            QueueUrl='https://queue.amazonaws.com/blablabla/my-queue',
            WaitTimeSeconds=10,
            MaxNumberOfMessages=10
        )
        print(response)
    except Exception as error:
        print(error)

这是我的 Dockerfile:

FROM python:3.6-alpine3.6
COPY ./requirements.txt /my_app/requirements.txt
COPY ./build/tmp/id_rsa /root/.ssh/id_rsa
RUN chmod 400 /root/.ssh/id_rsa && \
    ssh-keyscan -H bitbucket.org > /root/.ssh/known_hosts  && \
    pip3 install --no-cache-dir -r /my_app/requirements.txt  && \
    rm -rf /root/.ssh/
COPY ./src /my_app/src

WORKDIR /root/
EXPOSE 6092
VOLUME ["/root/"]
ENTRYPOINT ["python3", "/my_app/src/main.py"]

access_keysecret_key 在通过 docker 从 EC2 运行ning 时是正确的,因此它可能与凭据无关。我错过了什么?

原来我的应用无法解析任何域名。深入挖掘后,我发现 ec2 子网与 docker swarm 子网冲突,两者都使用 10.0.0.2 地址。解决方法是添加

nameserver 8.8.8.8
nameserver 8.8.4.4

到 /etc/resolv.conf。不是理想的解决方案,但它有效。