Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running shell script against Localstack in docker container

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.

like image 893
rschlachter Avatar asked Nov 30 '18 19:11

rschlachter


People also ask

How do I run a shell script in Dockerfile entrypoint?

Step 1: Create a script.sh file and copy the following contents. Step 2: You should have the script.sh is the same folder where you have the Dockerfile. Create the Dockerfile with the following contents which copy the script to the container and runs it part of the ENTRYPOINT using the arguments from CMD.

How do I stop a script from running in docker?

docker rm -f The final option for stopping a running container is to use the --force or -f flag in conjunction with the docker rm command. Typically, docker rm is used to remove an already stopped container, but the use of the -f flag will cause it to first issue a SIGKILL.


1 Answers

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:
      - '4563-4599:4563-4599'
      - '8055:8080'
    environment:
      - SERVICES=s3
      - DEFAULT_REGION=us-east-1
    volumes:
      - './localSetup.sh:/docker-entrypoint-initaws.d/make-s3.sh'

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

like image 194
krtsh Avatar answered Oct 06 '22 17:10

krtsh