jadadad
jadadad

Reputation: 51

Error when deoplying flask & socket-io script with docker

first of all to clarify some things:

  1. This python script works perfectly on my windows machine(without docker)
  2. I am also using virtualenv on my local machine
  3. while running on my machine, I can easily connect to to the socket server from my android phone(websocket tester app)

So now I am trying to run this websocket script(Flask & SocketIO) with docker on my ubuntu server on cloud(digital ocean).

My dockers commands for deploying this:

docker build -t websocketserver .

docker run -d -p 5080:8000 --restart always --name my_second_docker_running websocketserver

The script runs fine, BUT when i try to connect to it(from my phone), I get some errors when typing the command: "docker logs --tail 500 my_second_docker_running"

The error is:

    Traceback (most recent call last):
  File "/opt/company/project/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 134, in handle
    self.handle_request(listener, req, client, addr)
  File "/opt/company/project/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 175, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
TypeError: __call__() takes 1 positional argument but 3 were given

My requirements.txt:

Flask==1.1.1
Flask-SocketIO==3.0.1
aiohttp-cors==0.7.0
asyncio==3.4.3
gunicorn==20.0.4

My dockerfile:

FROM ubuntu:latest
MAINTAINER raxor2k "xxx.com"
RUN apt-get update -y

#RUN apt-get install -y python3-pip build-essential python3-dev
RUN apt-get install -y build-essential python3-dev python3-venv

COPY . /app
WORKDIR /app


RUN python3 -m venv /opt/company/project/venv
RUN /opt/company/project/venv/bin/python -m pip install -r requirements.txt


#ENTRYPOINT ["gunicorn"]
ENTRYPOINT ["/opt/company/project/venv/bin/gunicorn"]
CMD ["main:app", "-b", "0.0.0.0"]

and finally, my main.py file:

from aiohttp import web
import socketio
import aiohttp_cors
import asyncio


import asyncio as aio
import logging

# creates a new Async Socket IO Server
sio = socketio.AsyncServer()
# Creates
app = web.Application()
sio.attach(app)


# AIOSerial now logs! uncomment below for debugging
logging.basicConfig(level=logging.DEBUG)


async def index(request):
    with open('index.html') as f:
        print("Somebody entered the server from the browser!")
        return web.Response(text=f.read(), content_type='text/html')



@sio.on("android-device")
async def message(sid, data):
    print("message: ", data)

@sio.on("device-id")
async def message(sid, android_device_id):
    print("DEVICE ID: ", android_device_id)


@sio.on("disconnected-from-socket")
async def message(sid, disconnected_device):
    print("Message from client: ", disconnected_device)


async def send_message_to_client():
    print("this method got called!")
    await sio.emit("SuperSpecialMessage", {"Message from server:": "MESSAGE FROM SENSOR"})




# We bind our aiohttp endpoint to our app
# router
cors = aiohttp_cors.setup(app)
app.router.add_get('/', index)

# We kick off our server
if __name__ == '__main__':
    print("websocket server is running!")
    the_asyncio_loop = asyncio.get_event_loop()

    run_the_websocket = asyncio.gather(web.run_app(app))


    run_both_loops_together = asyncio.gather(run_the_websocket)

    results = the_asyncio_loop.run_until_complete(run_both_loops_together)

Could someone please help me solve this issue? could perhaps someone here try running this code yourself to see if you get the same error?

Upvotes: 1

Views: 699

Answers (1)

jadadad
jadadad

Reputation: 51

I decided to follow this example instead: https://github.com/miguelgrinberg/Flask-SocketIO

It works pretty much the same as my code and everything is fine now.

Upvotes: 1

Related Questions