Capturing Output of Python Script Run Inside a Docker Container

How to run python script in docker container?

Write a script named start.sh like this:

python3 /home/bob/test1/bin/start.py &> /home/bob/test1/status.log

Then update your docker file as follow:

FROM ubuntu:20.04

RUN mkdir /home/bob
WORKDIR /home/bob

RUN apt-get install -y python3 python3-pip
RUN echo "pyodbc" > /tmp/req.txt
RUN pip3 install -r /tmp/req.txt

# Will attached to /home/bob/folder1 in container (folder0 contain files and folder with test1 name)

ADD ./folder0 ./folder1
RUN chmod ug+x /home/bob/test1

COPY start.sh .
RUN chmod a+x start.sh

CMD ./start.sh

Python app does not print anything when running detached in docker

Finally I found a solution to see Python output when running daemonized in Docker, thanks to @ahmetalpbalkan over at GitHub. Answering it here myself for further reference :

Using unbuffered output with

CMD ["python","-u","main.py"]

instead of

CMD ["python","main.py"]

solves the problem; you can see the output (both, stderr and stdout) via

docker logs myapp

now!



Related Topics



Leave a reply



Submit