Docker-compose: How to access Localstack resources both from container and host, using same network address

Viewed 5515

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]

3 Answers

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

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

127.0.0.1 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 :

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
Related