[英]Docker-compose: How to access Localstack resources both from container and host, using same network address
I'm using docker-compose to run a python API and a Localstack instance in 2 separate containers, for local development.我正在使用 docker-compose 在 2 个单独的容器中运行 python API 和 Localstack 实例,用于本地开发。
The API has an endpoint which generates a presigned AWS S3 URL and redirects the user, in order to load images directly from S3. API 有一个端点生成预签名的 AWS S3 URL 并重定向用户,以便直接从 S3 加载图像。
In local development, the API instantiates a boto3 client using the address of the localstack container as a custom endpoint url (ie: boto3.client("s3",endpoint_url="http://localstack:4566")
) which allows the API to access resources within the localstack container.在本地开发中,API 使用 localstack 容器的地址作为自定义端点 url(即:
boto3.client("s3",endpoint_url="http://localstack:4566")
)实例化一个 boto3 客户端,它允许 API访问 localstack 容器内的资源。
The problem is that the presigned URL returned by the boto3 client uses the localstack
address, and the browser cannot load it, since the localstack resources are exposed to the host machine, at http://localhost:4566
.问题是 boto3 客户端返回的预签名 URL 使用了
localstack
地址,浏览器无法加载它,因为 localstack 资源暴露给主机,位于http://localhost:4566
。
If I try to set the aws resources endpoint url to localhost
in the boto3 client instantiation, then the API, which is running inside of a container, will look for AWS resources within it OWN CONTAINER's localhost, and not the host machine, where the localstack resources are exposed.如果我尝试在 boto3 客户端实例化中将 aws 资源端点 url 设置为
localhost
,那么在容器内运行的 API 将在其中查找 AWS 资源 OWN CONTAINER 的本地主机,而不是主机,其中 localstack资源暴露。
Is there any way to access localstack resources, running in a docker container, from both the host machine's browser AND a different container, using the same address?有什么方法可以使用相同的地址从主机的浏览器和不同的容器访问在 docker 容器中运行的 localstack 资源?
[Edit] I'm using docker on mac, in case that changes anything [/Edit] [编辑] 我在 mac 上使用 docker,以防发生任何变化 [/编辑]
Perhaps you can give host.docker.internal:4566
a try, since most likely the localstack service will be the only one listening to it.也许您可以尝试一下
host.docker.internal:4566
,因为很可能只有 localstack 服务会监听它。
I'm not sure, but I don't know in your local API call why you are referring to your endpoint as endpoint_url="http://localstack:4566"
the whole point is your localstack is running at endpoint localhost
and exposed to port 4566
http://localhost:4566
so your local client connection should be like this:我不确定,但我不知道在您的本地 API 调用中为什么您将端点称为
endpoint_url="http://localstack:4566"
重点是您的 localstack 在端点localhost
上运行并暴露于端口4566
http://localhost:4566
所以你的本地客户端连接应该是这样的:
boto3.client('s3', endpoint_url='http://localhost:4566')
Then your local client will able to connect to localstack s3 service.然后您的本地客户端将能够连接到 localstack s3 服务。 If you have a problem may be sharing your docker-compose will help, but in my case I refer to all my services using
localhost:4566
如果您有问题,可能共享您的 docker-compose 会有所帮助,但在我的情况下,我使用
localhost:4566
引用我的所有服务
Update:更新:
OPTION1: To run or create any resources while staying in the same container, I would prefer to go with docker-entrypoint-initaws.d
=> create a script that will be executing any CLI command to create the resources with endpoint as loclahost:4566
and then attach to your volume tag for docker-compose.选项1:要在同一个容器中运行或创建任何资源,我更喜欢 go 与 docker
docker-entrypoint-initaws.d
=> 创建一个脚本,该脚本将执行任何 CLI 命令以创建端点为loclahost:4566
的资源然后附加到 docker-compose 的卷标签。
another option is like this:另一种选择是这样的:
OPTION2:选项2:
local-stack:
.........
.........
networks:
- my_awesome_network
setup-resources:
image: mesosphere/aws-cli
environment:
AWS_ACCESS_KEY_ID: SOMESECRETACCESS
AWS_SECRET_ACCESS_KEY: SOMESECRETKEY
AWS_DEFAULT_REGION: eu-west-1
entrypoint: /bin/sh -c
command: >
"
# SIMPLE WRITE AN WHILE LOOP USING CURL COMMAND FOR http://localstack:4566
sleep 10;
aws sqs create-queue --endpoint-url=http://localstack:4566 --queue-name my_queue;
"
networks:
- my_awesome_network
depends_on:
- local-stack
You can add this to your /etc/hosts
file to resolve localstack
as localhost:您可以将此添加到您的
/etc/hosts
文件以将localstack
解析为本地主机:
127.0.0.1 localstack
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.