1

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.

1
  • It seems fine. it works thought. I ran nc two times to port 5000, and it accepts. Commented Feb 3, 2019 at 2:00

1 Answer 1

1

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..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.