My Flask app works locally when I run flask run -p 8000
but when I try to run this in Docker my SocketIO events don't seem to be getting through from the server to the client.
Here's an example app to show what I mean:
Flask app:
import flask
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
import requests
import logging
import time
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, logger=False, engineio_logger=False)
# Flask App
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('hello')
def connected(message):
data = message
print('Message received from browser:', data)
# Thanks I got your message
emit('reply', {'data': 'Hello browser. I received your message.'})
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8000)))
Dockerfile:
FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7
RUN apk add g++
RUN apk add linux-headers
RUN apk add libc-dev
RUN apk add musl-dev
COPY requirements.txt /
RUN python -m pip install --upgrade pip
RUN pip3 install numpy
RUN pip install -r /requirements.txt
COPY . /
WORKDIR /
ENV PORT 8000
RUN chmod +x ./gunicorn_starter.sh
ENTRYPOINT ["sh","./gunicorn_starter.sh"]
gunicorn_starter.sh:
#!/bin/bash
gunicorn --chdir / app:app -w 2 --threads 2 --workers 1 -b 0.0.0.0:8000
It runs really slow from Docker and the SocketIO events don't seem to be registering. This is the error I see:
[2022-01-27 20:35:31 +0000] [9] [ERROR] Error handling request /socket.io/?EIO=3&transport=websocket&sid=7862ffa61dcc4c1abbf38c368975d2b9
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/gunicorn/workers/gthread.py", line 279, in handle
keepalive = self.handle_request(req, conn)
File "/usr/lib/python3.6/site-packages/gunicorn/workers/gthread.py", line 336, in handle_request
resp.close()
File "/usr/lib/python3.6/site-packages/gunicorn/http/wsgi.py", line 409, in close
self.send_headers()
File "/usr/lib/python3.6/site-packages/gunicorn/http/wsgi.py", line 325, in send_headers
tosend = self.default_headers()
File "/usr/lib/python3.6/site-packages/gunicorn/http/wsgi.py", line 306, in default_headers
elif self.should_close():
File "/usr/lib/python3.6/site-packages/gunicorn/http/wsgi.py", line 229, in should_close
if self.status_code < 200 or self.status_code in (204, 304):
AttributeError: 'Response' object has no attribute 'status_code'