nginxsupervisorddaphne

Daphne + Supervisor inside Docker Container can't access my application


I'm trying to scale my Django app that uses Daphne server inside Docker container with Supervisor because Daphne has no workers. I read on the internet that it should be done that way but I didn't find any explanation of concept and the documentation is very obscure.

I managed to run it all inside container, logs are okay. I firstly run my app without supervisord with multiple containers and it worked fine. That is, I hosted multiple instance of same app in multiple containers because of redundancy. Then I read that I could run multiple processes of my app using supervisor inside container. So I managed to run app with supervisord and daphne inside container, I get logs that app is running, but I can't access it from my browser as I could when I had only one Daphne process per container without supervisord.

UPDATE: I can even curl my application inside of container when I use curl localhost:8000, but I can't curl it by container's IP address nor inside, nor outside of the container. That means that it's not visible outside of container despite container's port being exposed in docker-compose file.

I'm getting

502 Bad Gateway
nginx/1.18.0

My supervisord config file looks like this:

[supervisord]
    nodaemon=true
    [supervisorctl]
    
    [fcgi-program:asgi]
    User=root

    # TCP socket used by Nginx backend upstream
    socket=tcp://localhost:8000

    # Directory where your site's project files are located
    directory=/app
    
    # Each process needs to have a separate socket file, so we use process_num
    # Make sure to update "mysite.asgi" to match your project name
    command= /usr/local/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --endpoint fd:fileno=0     --access-log - --proxy-headers WBT.asgi:application

    # Number of processes to startup, roughly the number of CPUs you have
    numprocs=4
    
    # Give each process a unique name so they can be told apart
    process_name=asgi%(process_num)d
    
    # Automatically start and recover processes
    autostart=true
    autorestart=true

    # Choose where you want your log to go
    stdout_logfile=/home/appuser/supervisor_log.log
    redirect_stderr=true

I can't see why NGINX throws 502 error.
This configuration worked until I introduced supervisor.
My Nginx is also inside its own docker container.

upstream django_daphne{
    
    hash $remote_addr consistent;
    
    server django_daphne_1:8000;
    server django_daphne_2:8000;
    server django_daphne_3:8000;
}

server {
    
    server_name xxx.yyy.zzz.khmm;
    listen 80;
    client_max_body_size 64M;
    location = /favicon.ico { access_log off; log_not_found off; }
    
    location / {
        proxy_pass http://django_daphne;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #Websocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
    
    location /api/ {
        proxy_pass http://api_app:8888;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }
}

Solution

  • Okay! I found out what is a problem. Instead of

    socket=tcp://localhost:8000
    

    it has to be

    socket=tcp://0.0.0.0:8000
    

    so that it can be accessed outside of the container.