Running daphne in Docker without supervisor
Currently, I'm running daphne using supervisor. This is my supervisor config:
[supervisord]
user = root
nodaemon = true
[fcgi-program:daphne]
socket=tcp://api:9000
directory=/home/docker/api
ommand=./.docker/services/api/files/startup-operations.sh && daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --proxy-headers main.asgi:application
numprocs=2
process_name=asgi%(process_num)d
autostart=true
autorestart=false
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
redirect_stderr=true
killasgroup=true
startsecs=0
exitcodes=0
Here, the channels docs give exactly one example, and it's using supervisor: https://channels.readthedocs.io/en/latest/deploying.html
However, I have read this: https://advancedweb.hu/supervisor-with-docker-lessons-learned/
...which advocates for not using supervisor in docker, and I would agree with the article.
One problem with using supervisor that the article doesn't discuss is that I have an in-memory database that I need to be available for the main application.
The above config file is just one program that runs a startup-operations.sh
script, but before, it used to be two programs: One program loaded the data into memory, and the other program ran the main application.
Of course, I could load that data into memory in startup-operations.sh
and be done with it, but that doesn't remove supervisor from my image. It seems that if I'm just running one series of commands, I should be able to not use supervisor in my image.
I also had errors resulting from running out of space in my limited EC2 instance that both builds and runs this image (I know, it would be ideal to build the image locally and upload it to ECR, then run it on the server, but it's a testing server and we don't have that kind of capacity right now), and I wanted to reduce the storage space required to both build the image and run the container. Removing supervisor from the build and container would help with that.
Considering the linked article, the problem with sharing memory between supervisor programs, and the storage space limitations, I decided to try remove supervisor from both the build process and the final image of my dockerized application.
I used to have this CMD
directive in my Dockerfile:
CMD ["supervisord", "--nodaemon", "-c", "/etc/supervisor/conf.d/supervisor.conf"]
I now have this CMD
directive in my Dockerfile:
CMD ["/bin/bash", "-c", "mkdir -p /run/daphne && /home/docker/api/.docker/services/api/files/startup-operations.sh && daphne -u /run/daphne/daphne.sock --fd 0 --proxy-headers main.asgi:application"]
This builds and runs fine, but when I try to access an endpoint (such as https://host/admin/
), I get a 502 Bad Gateway error from nginx, and this error shows in the logs for the main application container:
daphne.server CRITICAL Listen failure: [Errno 88] Socket operation on non-socket
The server shuts down as a result.
I figure it has to do with this line in the original supervisor config:
socket=tcp://api:9000
Is there a line I can add to me CMD
directive to create this socket and avoid this error?
By the way, this is the contents of startup-operations.sh
:
#!/usr/bin/env bash
echo Waiting for the database to be ready...
./wait-for-db.sh
echo Collecting static files...
python manage.py collectstatic --no-input
echo Done.
echo Running migrations...
python manage.py migrate
echo Done.
echo Creating admin user...
python manage.py create_admin_user
echo Done.
# NOTE: This is the in-memory database.
# We are using local memory to store vectors in development/testing
# and a separate server in staging/production.
echo Initializing vector store...
python manage.py initialize_vector_store
echo Done.
echo Startup operations completed successfully!
exit 0