简体   繁体   English

Docker-compose:如何使用相同的网络地址从容器和主机访问 Localstack 资源

[英]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.

相关问题 从主机访问 mariadb docker-compose 容器 - Access mariadb docker-compose container from host-machine 文件未在docker-compose中从容器同步到主机 - File not being synced from container to host in docker-compose 如何使用 docker-compose 设置一个容器以允许它的整个卷访问另一个容器 - how to set one container to allow it's entire volume access to another container using docker-compose 如何从 Docker 容器内部访问 IFCONFIG 的网络地址? - How to access network address from IFCONFIG from inside a Docker Container? 使用docker-compose up与docker-compose run从容器内部执行容器中的命令 - executing commands in containers from within a container using docker-compose up vs docker-compose run docker-compose 如何从另一个项目访问模块? - docker-compose how to access module from another project? 无法从 docker-compose 的另一个容器访问 api 的 docker 容器 - docker container of api not accessible from another container of docker-compose 使用单独的 docker-compose yaml 时,如何从 PyMongo 访问 MongoDB? - How to access MongoDB from PyMongo when using separate docker-compose yaml's? 如何以与 docker-compose 相同的方式在远程服务器上运行 Docker 容器? - How can I run a Docker container on a remote server in the same manner as docker-compose up? Output 来自 Python 在 Docker 上运行并返回到主机 ZBAEDB53E845AE71F13945AE9Z05 - Output from Python running on Docker back to host with docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM