【问题标题】:Accessing local SQS service from another docker container using environment variables使用环境变量从另一个 docker 容器访问本地 SQS 服务
【发布时间】:2019-10-09 22:13:53
【问题描述】:

我有一个烧瓶应用程序,它需要在命中端点时与 SQS 服务进行交互。 我在本地使用 docker image sukumarporeddy/sqs:fp 模拟 SQS 服务,其基本镜像是 https://github.com/vsouza/docker-SQS-local,并在配置中添加了两个队列。

我需要从另一个作为 app_service 运行的 app 访问此服务。这两个服务使用 docker-compose.yml 文件运行,其中我提到了两个服务。

app_service

sqs_service

在构建 app 映像时,我将环境变量设置为以 QUEUE_ENDPOINT=http://sqs_service:9324 访问 sqs_service。但是当我尝试访问 sqs_service 应用程序时,它说队列端点无效。

我正在使用 boto3 连接到本地 sqs_service

boto3.client('sqs', endpoint_url=os.getenv("QUEUE_ENDPOINT"), region_name='default')

这是 docker-compose.yml 文件。

  app_service:
    container_name: app_container
    restart: always
    image: app
    build: 
      context: ./dsdp
      dockerfile: Dockerfile.app.local
    ports:
      - "5000:5000"
    env_file:
        - ./local_secrets.env
    command: flask run --host=0.0.0.0 --port 5000

  sqs_service:
    container_name: sqs_container
    image: sukumarporeddy/sqs:fp
    ports:
      - "9324:9324"

local_secrets.env:

QUEUE_ENDPOINT=https://sqs_service:9324
FEEDER_QUEUE_URL=https://sqs_service:9324/queue/feeder
PREDICTION_QUEUE_URL=https://sqs_service:9324/queue/prediction
AWS_ACCESS_KEY_ID=''
AWS_SECRET_ACCESS_KEY=''

我在尝试向本地运行的 SQS 服务发送消息时遇到错误。

值错误

ValueError:无效端点:https://sqs_service:9324

我在哪里犯错了?

【问题讨论】:

    标签: docker flask docker-compose boto3 amazon-sqs


    【解决方案1】:

    我认为您的 docker-compose 文件中没有 SSL 配置,因此问题可能出在 https 上。尝试将https 更改为http

    QUEUE_ENDPOINT=http://sqs_service:9324
    FEEDER_QUEUE_URL=http://sqs_service:9324/queue/feeder
    PREDICTION_QUEUE_URL=http://sqs_service:9324/queue/prediction
    

    另外,尝试通过名称调用 SQS 容器,而不使用 ENV 进行调试。

    import boto3
    sqs=boto3.client('sqs', endpoint_url="http://sqs_service:9324", region_name='default')
    # Create a SQS queue
    response = sqs.create_queue(
        QueueName='SQS_QUEUE_NAME',
        Attributes={
            'DelaySeconds': '60',
            'MessageRetentionPeriod': '86400'
        }
    )
    
    print(response['QueueUrl'])
    
    

    使用alpine-sqs 图像测试。

    【讨论】:

    • 即使将https 更改为http 后仍然出现同样的错误。 ValueError ValueError: Invalid endpoint: http://sqs_service:9324
    • 我不确定图像是否与图像有关。但用hub.docker.com/r/roribio16/alpine-sqs 测试并且工作正常
    • 您能否确认该服务正在您的容器中运行?
    • 让我尝试使用您建议的新图像。
    • 你让它工作真是太好了。正如我用sqs=boto3.client('sqs', endpoint_url="http://localhost:9324", region_name='default') 测试的那样,服务名称可能就是这种情况。脚本在主机上,sqs 在容器中运行
    【解决方案2】:

    不知何故,我无法使用环境变量中提到的服务名称来使用 SQS 队列。 这就是我所做的。

    从环境变量中获取服务名称,使用python中的socket库获取服务的IP地址,并使用IP地址格式化并创建QUEUE端点url。

    import socket
    queue_endpoint_service = os.getenv("QUEUE_ENDPOINT_SERVICE")
    queue_endpoint_port = os.getenv("QUEUE_ENDPOINT_PORT")
    feeder_queue = os.getenv("FEEDER_QUEUE")
    prediction_queue = os.getenv("PREDICTION_QUEUE")
    queue_endpoint_ip = socket.gethostbyname(queue_endpoint_service)
    queue_endpoint = f"http://{queue_endpoint_ip}:{queue_endpoint_port}"
    mc = boto3.client('sqs', endpoint_url=queue_endpoint, region_name='default')
    feeder_queue_url = f"{queue_endpoint}/queue/{feeder_queue}"
    prediction_queue_url = f"{queue_endpoint}/queue/{prediction_queue}"
    

    我现在可以通过点击烧瓶应用程序中的端点来发送消息。

    注意: 还使用了 Adiii 提到的新 docker 镜像。不再使用旧图像。

    【讨论】:

      猜你喜欢
      • 2018-10-21
      • 2019-05-09
      • 2020-12-22
      • 2015-07-07
      • 2019-01-19
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      相关资源
      最近更新 更多