简体   繁体   中英

Unable to connect multiple python socket.io inside docker

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.

It turns out that the docker logs are delayed until the python program stop. So I never saw the print because the program, well, never stop. The solution is to put this env variable in the docker-compose file:

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

So now I can see the print that confirm connection..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM