Initialization hook not working?

I’m trying to run localstack 2.1.0 along with testcontainers. I am trying to run an init script to create a SQS queue, but the script never gets executed. This is what I tried:

    static final LocalStackContainer localstack =
            new LocalStackContainer(DockerImageName.parse("localstack/localstack:2.1.0"))
                    .withCopyFileToContainer(
                            MountableFile.forClasspathResource("localstack/init.sh"), 
                            "/docker-entrypoint-initaws.d/init.sh");

This is the content of the script:

#!/bin/sh

awslocal sqs create-queue --queue-name user_created_queue

In contrast, the queue is created if I do it programatically with

execInContainer("awslocal", "sqs", "create-queue", "--queue-name", "user_created_queue");

What could be the cause of the script not getting run by the container?

hi @edubkn,
A few things:
Firstly, /docker-entrypoint-initaws.d/ has been removed with LocalStack v2. You can find out more about our init hooks here: Initialization Hooks | Docs.
Secondly, you want to make sure the script is executable, but running chmod +x init.sh on the file first will not help because the withCopyFileToContainer method does not preserve the mode. What you want to do is use the method that allows you to explicitly set the mode for the file: forClasspathResource(String resourceName, Integer mode) .
That means your code will look like this:

static final LocalStackContainer localStack =
      new LocalStackContainer(DockerImageName.parse("localstack/localstack:2.1.0"))
          .withCopyToContainer(
              MountableFile.forClasspathResource("localstack/init.sh", 775),
              "/etc/localstack/init/ready.d/init.sh");

Notice that the file mode is set to 775. If you skip this, you will get a Permission denied error in the container upon execution. The script is also executed in the ready lifecycle phase of LocalStack (the replacement of /docker-entrypoint-initaws.d).

Best,
Anca

hi @edubkn,
I’m back with a little update: while I had trouble getting the file permissions recognized by the container, it turns out I just had to clean my target folder. My tests were using the old permissions of the file because there was no new build. You don’t have to explicitly tell Testcontainers to set a certain mode, but it is an option.

Thanks, I can attest that /etc/localstack/init/ready.d indeed works, although I really have to add the file permisson (775).

2 Likes