使用环境变量从另一个Docker容器访问本地SQS服务 [英] Accessing local SQS service from another docker container using environment variables

查看:155
本文介绍了使用环境变量从另一个Docker容器访问本地SQS服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个flask应用程序,每当命中一个端点时,该应用程序都需要与SQS服务进行交互. 我正在使用基本映像为 https://的docker映像sukumarporeddy/sqs:fp在本地模拟SQS服务. github.com/vsouza/docker-SQS-local ,并在配置中添加了另外两个队列.

I have a flask application which needs to interact with an SQS service whenever an endpoint is hit. I'm mimicing the SQS service locally using docker image sukumarporeddy/sqs:fp whose base image is https://github.com/vsouza/docker-SQS-local with two more queues added in configuration.

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

I need to access this service from another app which is run as app_service. These two services are run using docker-compose.yml file where I mentioned two services.

app_service

sqs_service

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

While building the app image, I'm setting environment variables to access the sqs_service as QUEUE_ENDPOINT=http://sqs_service:9324. But when I try to access the sqs_service the app, it is saying invalid queue endpoint.

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

I'm using boto3 to connect to the local 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服务时遇到的错误.

Error that I'm getting when trying to send messages to the SQS service that is running locally.

ValueError

ValueError

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

ValueError: Invalid endpoint: https://sqs_service:9324

我在哪里犯错?

推荐答案

以某种方式,我无法使用环境变量中提到的服务名称来使用SQS队列. 这是我所做的.

Somehow I was not able to use the SQS queue using the service name mentioned in the environment variables. This is what I did instead.

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

Got the service name from environment variables, got the IP address of the service using socket library in python and used the IP address to format and create the QUEUE endpoint 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}"

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

I'm able to send the messages now by hitting the endpoints in flask app.

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

Note: Also used the new docker image which Adiii mentioned. Not using the old image anymore.

这篇关于使用环境变量从另一个Docker容器访问本地SQS服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆