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
Anaconda: Disable Prompt Change
Find Broken Symlinks with Python
Better Way to Script Usb Device Mount in Linux
Linux/Python: Encoding a Unicode String for Print
Pyserial Works Fine in Python Interpreter, But Not Standalone
Running Python Script as a Systemd Service
/Usr/Bin/Env: Python2: No Such File or Directory
Python Linux Selenium: Chrome Not Reachable
Set the Hardware Clock in Python
How to Install Lxml for Python Without Administative Rights on Linux
A Way to "Listen" for Changes to a File System from Python on Linux
High Kernel CPU When Running Multiple Python Programs
Send Sigint in Python to Os.System
Running a Python Script Using Cron
How to Write Dataframe to Postgres Table
Letsencrypt Importerror: No Module Named Interface on Amazon Linux While Renewing