0

I have a flask application that is very simple, without any operation, the code is as follows, just for testing:

from flask import Flask
app1 = Flask(__name__)
@app1.route('/', methods=['POST'])
def send():
    print(1111111111111111)
    a = "ok"
    print(1111111111111111)
    return a
app1.run(debug=False,host='0.0.0.0',port=8060)

Then I built him into a image through Dockerfile:

From python:3.6.12
COPY ./a.py /usr/
RUN pip3 install -i https://mirrors.aliyun.com/pypi/simple/ flask
CMD python3 /usr/a.py

My understanding is that when I pass docker run -p12345:8060 -d image_name, Then I access the interface through the following simple code, I think the interface should output two lines of "111111111111" logs, but it is not

import requests

url="http://127.0.0.1:12345"
data = {
    "server_id": 1,
}
header = {
    "Content-Type": "application/json",
    "charset": "utf-8"
}
res = requests.post(url=url,json=data,headers=header)
print(res.text)

enter image description here

We can see in the picture when the startup parameter is -it, there is print, but when the parameter is -d, I get the log through docker logs, but I don't see print. The CMD of my image is to start this python code, so the print in the code should be output on the docker console, so no matter I start docker through -it or -d, the docker logs command can get the output of print.

Isn't it?

Andrzej Sydor
  • 1,373
  • 4
  • 13
  • 28
gophergfer
  • 261
  • 4
  • 9

1 Answers1

2

Because Python buffer their output, print doesn't work correctly. To disable it, pass -u option to python.

CMD python3 -u /usr/a.py

This post helped me, and will help you.

Python app does not print anything when running detached in docker - Stack Overflow

Akihito KIRISAKI
  • 1,243
  • 6
  • 12