0

I have a python program that must run in a docker container. When I execute the program in my workspace the program run well. I made a custom Dockerfile from a python image, this image have the same packages in the same version that I have in my workspace. When I run the container using compose the container starts but when I call a specific function the program seems not to run. I put a print before this function call and the print works only when I comment the func call (the func call is after the print), when I call the function the print don't works.

My dockerfile:

FROM python:3.8.5-slim

RUN apt update \
    && apt upgrade -y\
    && apt install -y libsm6 libxext6 libxrender-dev libglib2.0-0 \
    && pip install redis==3.5.3 opencv-contrib-python==4.2.0.32 imutils==0.5.3 scikit-image==0.17.2 \
    && mkdir src
    
WORKDIR /src

CMD ["python"]

My compose:

version: '3.1'  

services:  
  redis:
    image: 'redis:alpine'
    command: redis-server --requirepass 123
    ports:
      - '6379:6379'

  doorState-peopleCounter:
    build:
      context: .
      dockerfile: Dockerfile
    image: custom
    volumes:
      - /media/jose/Arquivos/Argos/rasp-services/movementdetectionwindowsdoor:/src
    command: ["python", "Tracker.py"]

With this code the print doesn't works:

print('[INFO] - Iniciando monitoramento da porta')
tracking.count(source='videos/subindo.mp4')

When I comment the func call the print works:

print('[INFO] - Iniciando monitoramento da porta')
#tracking.count(source='videos/subindo.mp4')
jvss
  • 33
  • 6
  • Do you see any errors in the logs of your container? What does `docker-compose logs doorState-peopleCounter` say? – Sergei Feb 05 '21 at 14:12
  • Your compose file runs `python Tracker.py`; your Dockerfile only runs `python`. – chepner Feb 05 '21 at 14:13
  • The logs dosn' t show any errors – jvss Feb 05 '21 at 14:15
  • Dockerfile CMD is "python" but in the compose file I run "python Tracker.py". The program only don't run when I call tracking.count() – jvss Feb 05 '21 at 14:17

3 Answers3

0

I don't know why, but the container logs only show the prints of the program when the python program ends. So the problem was that I wasn't waiting the program finish and the i started to think that the program was not running.

jvss
  • 33
  • 6
0

Change

FROM python:3.8.5-slim

to

FROM python:3.8.5

You can add regular python, not Slim, it may be large, but it will do the trick.

bguiz
  • 24,927
  • 44
  • 149
  • 234
Ali Gökkaya
  • 124
  • 3
  • 18
0

This is python's output buffering. It's a performance feature. You can disable it with the environment variable PYTHONUNBUFFERED=1 or by running python with the -u flag. You can also call print with the flush keyword argument.

print('[INFO] - Iniciando monitoramento da porta', flush=True)
D Hudson
  • 876
  • 3
  • 10