I'm trying to run sqs with localstack using docker compose to execute application tests but localstack container not starting.
localstack:
image: localstack/localstack
container_name: localstack_main
ports:
- "4566:4566"
- "4571:4571"
- "8055:8080"
environment:
- SERVICES=sqs
- DEBUG=1
- DATA_DIR=/tmp/localstack/data
volumes:
- "./localstack_data:/tmp/localstack" # Mounts the local directory to the container
- "/var/run/docker.sock:/var/run/docker.sock" # Mounts the docker socket
# tmpfs:
# - /tmp/localstack:exec,mode=777
Getting error in container logs
LocalStack supervisor: starting
LocalStack supervisor: localstack process (PID 15) starting
ERROR: 'rm -rf "/tmp/localstack"': exit code 1; output: b"rm: cannot remove '/tmp/localstack': Device or resource busy\n"
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/opt/code/localstack/localstack/runtime/main.py", line 28, in <module>
main()
File "/opt/code/localstack/localstack/runtime/main.py", line 20, in main
infra.start_infra(asynchronous=False)
File "/opt/code/localstack/localstack/services/infra.py", line 220, in start_infra
files.rm_rf(config.dirs.tmp)
File "/opt/code/localstack/localstack/utils/files.py", line 214, in rm_rf
shutil.rmtree(path)
File "/usr/local/lib/python3.11/shutil.py", line 763, in rmtree
onerror(os.rmdir, path, sys.exc_info())
File "/usr/local/lib/python3.11/shutil.py", line 761, in rmtree
os.rmdir(path, dir_fd=dir_fd)
OSError: [Errno 16] Device or resource busy: '/tmp/localstack'
LocalStack supervisor: localstack process (PID 15) returned with exit code 1
LocalStack supervisor: exiting
How to fix ?
The error is coming from the localstack bootup code, which exists on /opt/code/localstack/localstack
, which attempts to clear the configured temp folder on startup.
This process will handle a permission error, but when you have mounted a file system, you receive a different error - Device busy
, which causes the code to not start up, as that exception is not caught by the cleanup code.
What it's implying, though, is that the temporary folder is really not a place to put your code/data. It indicates also that the entire folder will be purged every run, which sounds like a bad thing?
The 'basic' openstack docker-compose indicates the following:
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
So, it seems that using this pattern - mapping ./localstack_data
to /var/lib/localstack
is the right thing to do in this case:
localstack:
image: localstack/localstack
container_name: localstack_main
ports:
- "4566:4566"
- "4571:4571"
- "8055:8080"
environment:
- SERVICES=sqs
- DEBUG=1
- PERSISTENCE=1
volumes:
- "./localstack_data:/var/lib/localstack" # Mounts the local directory to the container
- "/var/run/docker.sock:/var/run/docker.sock" # Mounts the docker socket
Note - I have removed DATA_DIR
, and added PERSISTENCE
based on the localstack documentation, which states:
DATA_DIR
implicitly points to/var/lib/localstack/state
if persistence is enabled. UsePERSISTENCE=1
to enable persistence. IfDATA_DIR
is set, its value is ignored, a warning is logged andPERSISTENCE
is set to 1.