18

I've been using localstack to develop a service against locally. I've just been running their docker image via docker run --rm -p 4567-4583:4567-4583 -p 8080:8080 localstack/localstack

And then I manually run a small script to set up my S3 buckets, SQS queues, etc.

Now, I'd like to make this easier for others so I thought I'd just add a Dockerfile and docker-compose.yml file. Unfortunately, when I try to get this up and running, using docker-compose up I get an error that the command from my setup script can't connect to the localstack services.

make_bucket failed: s3://localbucket Could not connect to the endpoint URL: "http://localhost:4572/localbucket"

Dockerfile:

FROM localstack/localstack

#since this is just local dev set up, localstack doesn't require 
anything specific here.
ENV AWS_DEFAULT_REGION='[useast1]'
ENV AWS_ACCESS_KEY_ID='[lloyd]'
ENV AWS_SECRET_ACCESS_KEY='[christmas]'

COPY bin/localSetup.sh /localSetup.sh
COPY fixtures/notifications.json /notifications.json
RUN ["chmod", "+x", "/localSetup.sh"]
RUN pip install awscli

# expose service & web dashboard ports
EXPOSE 4567-4582 8080

ENTRYPOINT ["/localSetup.sh"]

docker-compose.yml

version: '3'
services:
  localstack:
    build: .
    ports:
      - "8080:8080"
      - "4567-4582:4567-4582"

localSetup.sh

#!/bin/bash

aws --endpoint-url=http://localhost:4572 s3 mb s3://localbucket
#additional similar calls but left off for brevity

I've tried switching localhost to 127.0.0.1 in my script commands, but I wind up with the same error. I'm probably missing something silly here.

rschlachter
  • 720
  • 1
  • 9
  • 20

2 Answers2

21

There is another way to create your custom AWS resources when localstack freshly starts up. Since you already have a bash script for your resources, you can simply volume mount your script to /docker-entrypoint-initaws.d/. So my docker-compose file would be:

localstack:
    image: localstack/localstack:latest
    container_name: localstack_aws
    ports:
      - '4566:4566'
    volumes:
      - './localSetup.sh:/etc/localstack/init/ready.d/init-aws.sh'

Also, I would prefer awslocal over aws --endpoint in the bash script, as it leverages the credentials work and endpoint for you.

georgiosd
  • 3,038
  • 3
  • 39
  • 51
krtsh
  • 409
  • 5
  • 13
  • This works perfect! I would like to initialize the bucket with a local csv for testing. Do you have any suggestions on how to do that? – thijsvdp Aug 11 '20 at 07:42
  • No answer needed anymore. I figured it out already :). I used volumes to exchange data and then run awslocal s3 cp /path/to/file s3://bucket/path/to/file. – thijsvdp Aug 11 '20 at 08:42
  • I just realized that that this means that the lines in the dockerfile for running chmod and copy of the localsetup.sh file can go. However we should also refer to the ./bin/localSetup.sh as it's looking for the local file location rather than on the docker container. – howard Jul 18 '23 at 07:05
1

try adding hostname to the docker-compose file and editing your entrypoint file to reflect that hostname.

docker-compose.yml

version: '3'
services:
  localstack:
    build: .
    hostname: localstack
    ports:
      - "8080:8080"
      - "4567-4582:4567-4582"

localSetup.sh

#!/bin/bash

aws --endpoint-url=http://localstack:4572 s3 mb s3://localbucket

This was my docker-compose-dev.yaml I used for testing out an app that was using localstack. I used the command docker-compose -f docker-compose-dev.yaml up, I also used the same localSetup.sh you used.

version: '3'
services:

  localstack:
    image: localstack/localstack
    hostname: localstack
    ports:
      - "4567-4584:4567-4584"
      - "${PORT_WEB_UI-8082}:${PORT_WEB_UI-8082}"
    environment:
      - SERVICES=s3
      - DEBUG=1
      - DATA_DIR=${DATA_DIR- }
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
    networks:
      - backend

  sample-app:
    image: "sample-app/sample-app:latest"
    networks:
      - backend
    links:
      - localstack
    depends_on:
      - "localstack"

networks: 
  backend:
    driver: 'bridge'
Seth
  • 446
  • 5
  • 10