7

I'm using docker-compose to run a python API and a Localstack instance in 2 separate containers, for local development.

The API has an endpoint which generates a presigned AWS S3 URL and redirects the user, in order to load images directly from 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.

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.

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.

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?

[Edit] I'm using docker on mac, in case that changes anything [/Edit]

1
  • The first thing you're going to need to fix is the fact that localstack is listening on 127.0.0.1 on your host. Any solution will probably require the service to be listening on all interfaces instead (0.0.0.0). I was writing up a longer answer, but as I don't have access to a Mac running Docker there ended up being too much conjecture. Because on a Mac (or Windows) Docker is running in a virtual machine the networking is different than e.g. on my Linux host. Commented Mar 23, 2021 at 21:35

3 Answers 3

3

Perhaps you can give host.docker.internal:4566 a try, since most likely the localstack service will be the only one listening to it.

Sign up to request clarification or add additional context in comments.

Comments

2

You can add this to your /etc/hosts file to resolve localstack as localhost:

127.0.0.1 localstack

Comments

-1

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 :

boto3.client('s3', endpoint_url='http://localhost:4566')

Then your local client will able to connect to localstack s3 service. 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

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.

another option is like this:

OPTION2:

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

2 Comments

I'm operating under this assumption: By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name. From: docs.docker.com/compose/networking So my services are accessible to my host machine at localhost:8000 but to each other at <container-name>:<port>. In localstack's case, the container name is localstack
Though I don't have a complete picture of your docker-compose file. But I think I got your point. I have updated my answers. if still you have questions, please feel free to post your docker-compose file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.