Docker Community Forums

Share and learn in the Docker community.

SocketIO Issue when running Flask app in Docker Container

I have a Flask application that uses SocketIO to fetch data from Postgres live.

The app works fine when I run this locally.

The problem arouses when I use docker-compose to host my Flask app in a container. My JS client and flask server is hosted into a single app and on the same container.

My socketio in JS is like this:

var socket = io().connect(window.location.protocol + '//' + document.domain + ':' + location.port);

Dockerfile:

# Using python 3.7 in Alpine
FROM python:3.6.5-stretch

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

RUN apt-get update -y && apt-get upgrade -y

# Install the dependencies from requirements
RUN pip install -r requirements.txt

# Tell the port number the container should expose
EXPOSE 8083

# Run the command
ENTRYPOINT ["./entry.sh"]

entry.sh:

#!/bin/sh
gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -b :8083 -w 5 run:app

My docker-compose is as such:

version: "3.8"

services:
  fortweet:
    container_name: fortweet
    build: ./
    env_file:
      - secret.env
    networks:
      - plutusnet
    ports:
      - 8083:8083
    restart: always

networks:
  plutusnet:
    name: plutus_network
    driver: bridge

I’ve also tried to use var socket = io.connect('http://public_ip_of_website:8083') but my socket connection still doesn’t work.

How it should normally work is when i run this locally and click a certain button, it executes this function in my JS:

$("#tweets-live-start").click(function(){
    if (is_streaming == true){
      alert("A stream is already running")
    }else{
      $.ajax({
        type: "POST",
        url : "/admin/startstream",
        data: {url : "print \"hello\""},
        contentType: 'application/json;charset=UTF-8'
      });
    }
  });

When my server gets the hello, it starts a tweet streaming and emits them through the socket. Then my socket captures them as such:

// Listens for tweets
socket.on('stream-results', function(results){

  // Insert tweets in divs
  $('#live-tweet-container').prepend(`
  <div class="row justify-content-md-center mt-3">
    <div class="col-md-2">
        <img width="56px" height="56px"  src="${results.profile_pic !== "" ? results.profile_pic : "/static/icons/profile-pic.png"}" class="mx-auto d-block rounded"  alt="">
    </div>
    <div class="col-md-8 my-auto">
      <div><b>${results.author}</b></div>
      <div>${results.message}</div>
    </div>
  </div>
  `);
});

But when i run it on docker, nothing happens.

When i check my browser JS console, it seems that it is polling with a bad request and i don’t know why:

index.js:83 GET http://th3pl4gu3.com:8083/socket.io/?EIO=3&transport=polling&t=NPYYrxr 400 (BAD REQUEST)

Here is my docker ps for more info:

46446efeb472   mervin16/fortweet:dev   "/bin/bash entry.sh"  About a minute ago   Up About a minute   0.0.0.0:8083->8083/tcp   fortweet

12b2bff36af0   postgres    "docker-entrypoint.s…"   2 hours ago          Up 2 hours          0.0.0.0:5432->5432/tcp   plutus

I don’t think this is an accessibility issue because i tried several telnet tests from each container and to each container also.

I checked logs of the docker container and it gives this:

fortweet    | [2020-12-26 15:18:55 +0000] [8] [INFO] Starting gunicorn 20.0.4
fortweet    | [2020-12-26 15:18:55 +0000] [8] [INFO] Listening at: http://0.0.0.0:8083 (8)
fortweet    | [2020-12-26 15:18:55 +0000] [8] [INFO] Using worker: geventwebsocket.gunicorn.workers.GeventWebSocketWorker
fortweet    | [2020-12-26 15:18:55 +0000] [11] [INFO] Booting worker with pid: 11
fortweet    | [2020-12-26 15:18:55 +0000] [12] [INFO] Booting worker with pid: 12
fortweet    | [2020-12-26 15:18:55 +0000] [13] [INFO] Booting worker with pid: 13
fortweet    | [2020-12-26 15:18:55 +0000] [14] [INFO] Booting worker with pid: 14
fortweet    | [2020-12-26 15:18:55 +0000] [15] [INFO] Booting worker with pid: 15
fortweet    | The client is using an unsupported version of the Socket.IO or Engine.IO protocols (further occurrences of this error will be logged with level INFO)
fortweet    | The client is using an unsupported version of the Socket.IO or Engine.IO protocols (further occurrences of this error will be logged with level INFO)
fortweet    | 172.16.0.1 - - [2020-12-26 15:19:57] "POST /admin/startstream HTTP/1.1" 204 170 0.023672
fortweet    | The client is using an unsupported version of the Socket.IO or Engine.IO protocols (further occurrences of this error will be logged with level INFO)
fortweet    | The client is using an unsupported version of the Socket.IO or Engine.IO protocols (further occurrences of this error will be logged with level INFO)
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:20] "GET /socket.io/?EIO=3&transport=polling&t=1608996021267-7 HTTP/1.1" 400 195 0.001418
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:20] "GET /socket.io/?EIO=3&transport=polling&t=1608996021267-7 HTTP/1.1" 400 195 0.001418
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:21] "GET /socket.io/?EIO=3&transport=polling&t=1608996021395-8 HTTP/1.1" 400 195 0.001625
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:21] "GET /socket.io/?EIO=3&transport=polling&t=1608996021395-8 HTTP/1.1" 400 195 0.001625
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:26] "GET /socket.io/?EIO=3&transport=polling&t=1608996026417-9 HTTP/1.1" 400 195 0.001367
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:26] "GET /socket.io/?EIO=3&transport=polling&t=1608996026417-9 HTTP/1.1" 400 195 0.001367
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:26] "GET /socket.io/?EIO=3&transport=polling&t=1608996027270-8 HTTP/1.1" 400 195 0.003811
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:26] "GET /socket.io/?EIO=3&transport=polling&t=1608996027270-8 HTTP/1.1" 400 195 0.003811
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:34] "POST /admin/startstream HTTP/1.1" 204 170 0.015831
fortweet    | 172.16.0.1 - - [2020-12-26 15:20:36] "GET /socket.io/?EIO=3&transport=polling&t=1608996036486-11 HTTP/1.1" 400 195 0.001096

Can anyone please help me ?