7

Hello I would like to use

print('Hello world')

to see what's going on with some variables, however im using Docker which means by default it's not printing anything. Can someone tell me how I can pass these print commands to the docker container ?

2
  • 1
    You can check docker logs to see output of the containers Commented Mar 16, 2019 at 18:26
  • 1
    Additionally, you could run the container in non-detached mode, i.e omit the -d flag when doing docker run and output should be printed to stdout Commented Mar 16, 2019 at 18:35

2 Answers 2

14

This is because Python buffers its output by default.

An easy way to change this behavior is to use the PYTHONUNBUFFERED=1 environment variable: docker run -e PYTHONUNBUFFERED=1 <your_image>

An other way is to call the python command with the -u option.

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

Comments

-3

Maybe you should run your container with the privileged option to share /dev (therefore, sharing /dev/stdout).

This is working for me:

print.py:

print("Hello world")

Dockerfile:

FROM python
ADD print.py /
ENTRYPOINT ["python",  "/print.py"]

Commands:

$ docker build -t python-test .
$ docker run -it --privileged python-test
Hello world

Hope it helps.

1 Comment

A simple print statement does not need the vast power over the host that --privileged gives (for example it gives the power to shut down the host). /dev/stdout is usually just a link to /proc/self/fd/1, which is to say, it's the process's normal standard output; you don't even need a device file to write there.

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.