2

I want to spin up a localstack docker container and run a file, create_bucket.sh, with the command

aws --endpoint-url=http://localhost:4566 s3 mb s3://my-bucket

after the container starts. I tried creating this Dockerfile

FROM: localstack/localstack:latest
COPY create_bucket.sh /usr/local/bin/
ENTRYPOINT []

and a docker-compose.yml file that has

version: '3.8'
services:
  localstack:
    image: localstack/localstack:latest
    environment:
      ...
    ports:
      - '4566-4583:4566-4583'
    command: sh -c "/usr/local/bin/create_bucket.sh"

but when I run

docker-compose up

the container comes up, but the command isn't run. How do I execute my command against the localstack container after container startup?

Dave
  • 15,639
  • 133
  • 442
  • 830

3 Answers3

4

You can use mount volume instead of "command" to execute your script at startup container.

volumes:
      - ./create_bucket.sh:/docker-entrypoint-initaws.d/create_bucket.sh

Also as they specify in their documentation, localstack must be precisely configured to work with docker-compose.

Please note that there’s a few pitfalls when configuring your stack manually via docker-compose (e.g., required container name, Docker network, volume mounts, environment variables, etc.)

In your case I guess you are missing some volumes, container name and variables.

Here is an example of a docker-compose.yml found here, which I have more or less adapted to your case

version: '3.8'

services:
  localstack:
    image: localstack/localstack
    container_name: localstack-example
    hostname: localstack
    ports:
      - "4566-4583:4566-4583"
    environment:
      # Declare which aws services will be used in localstack
      - SERVICES=s3
      - DEBUG=1
      # These variables are needed for localstack
      - AWS_DEFAULT_REGION=<region>
      - AWS_ACCESS_KEY_ID=<id>
      - AWS_SECRET_ACCESS_KEY=<access_key>
      - DOCKER_HOST=unix:///var/run/docker.sock
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - "${TMPDIR:-/tmp}/localstack:/tmp/localstack"
      - /var/run/docker.sock:/var/run/docker.sock
      - ./create_bucket.sh:/docker-entrypoint-initaws.d/create_bucket.sh

Other sources:

Mickael Rahmine
  • 372
  • 1
  • 7
2

If you exec into the container, the create_bucket.sh is not copied. I'm not sure why and I couldn't get it to work either.

However, I have a working solution if you're okay to have a startup script as your goal is to bring up the container and execute the creation of the bucket in a single command.

  1. Assign a name to your container in docker-compose.yml
version: '3.8'
services:
  localstack:
    image: localstack/localstack:latest
    container_name: localstack
    ports:
      - '4566-4583:4566-4583'
  1. Update your create_bucket.sh to use awslocal instead, it is already available in the container. Using aws cli with an endpoint-url needs aws configure as a pre-req.
awslocal s3 mb s3://my-bucket
  1. Finally, create a startup script that runs the list of commands to complete the initial setup.
docker-compose up -d
docker cp create_bucket.sh localstack:/usr/local/bin/
docker exec -it localstack sh -c "chmod +x /usr/local/bin/create_bucket.sh"
docker exec -it localstack sh -c "/usr/local/bin/create_bucket.sh"
  1. Execute the startup script
sh startup.sh

To verify, if you now exec into the running container, the bucket would have been created.

docker exec -it localstack /bin/sh
awslocal s3 ls

bucket created verification

Rohit P
  • 613
  • 5
  • 13
0

Try by executing below

docker exec Container_ID Your_Command
Shamith Wimukthi
  • 468
  • 3
  • 12
  • I'm not understanding -- I shoudl run this instead of "docker-compose.yml"? – Dave Aug 09 '22 at 20:16
  • This way, once the container is up., you can run any missing commands against the container from the host – Shamith Wimukthi Aug 10 '22 at 01:04
  • My goal is to bring up the container and execute the creation of the bucket in a single command, preferrably "docker-compose up," because that is a standard command that other developers will know. – Dave Aug 10 '22 at 13:12