Linked Questions

Popular Questions

Unable to connect multiple python socket.io inside docker

Asked by At

I built a docker-compose of a simple python3.6 container exposing port 5000. This container run a python server side script waiting for clients to connect. Here are the files:

Dockerfile:

FROM python:3.6-alpine

WORKDIR /app
CMD ["python","serveur.py"]

Docker-compose:

version: '2'
services:
  serveur:
    build:
      context: .
      dockerfile: Serveur
    ports:
      - "127.0.0.1:5000:5000"
    volumes:
      - "./app:/app"

serveur.py:

#!/usr/bin/python3 
import socket  
import threading

print("debut du programme")
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "0.0.0.0"
port = 5000
socket.bind((host, port))
socket.listen(5)

for i in range(2):
        print("ready to connect")
        a,b = socket.accept()
        print("Client connected")

socket.close()

Here is the issue:

-If I run the docker compose, my client cant connect on the server; the code seems to block.More over, none of the print are showing in the Docker logs. If I take the socket.accept() out of the loop, one client can connect and I see all the print in the logs. If I take the loop out of the code and I just align multiple socket.accept(), well, the code block.

I know the issue is with my Docker settings because if I run this script out of Docker, the serveur.py works perfectly.

Thanks guys for your time.

Related Questions