diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e514ac8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.6-alpine + +RUN mkdir /app +COPY my.py /app +WORKDIR /app + +ENTRYPOINT ["python3", "-u", "my.py"] + diff --git a/README.md b/README.md new file mode 100644 index 0000000..2af55ee --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ +Python I/O redirection with Docker example +=== + +Toy project experimenting how to use a Python app inside a Docker container to stream commands via STDIN +and receive responses via STDOUT just like you would do with a local Python app. + +This might help in deciding how to invoke apps embedded with Docker. + +Local invocation +=== + +Just for reference, this is what I want to achieve: + +$ cat | python3=== + +Toy project experimenting how to use a Python app inside a Docker container to stream commands via STDIN +and receive responses via STDOUT just like you would do with a local Python app. + +Local invocation +=== + +Just for reference, this is what I want to achieve: + + $ cat | python3 my.py + 1 2 3 + 6 + 10 20 30 + 60 + ^CTraceback (most recent call last): + File "my.py", line 11, in + for line in sys.stdin: + KeyboardInterrupt + my.py + +Building the Docker image +=== + + docker build -t mypy . + +Invoking it +=== + + $ cat | docker run --rm -i mypy + 1 2 3 + 6 + 10 20 30 + 60 + ^CTraceback (most recent call last): + File "my.py", line 12, in + for line in sys.stdin: + KeyboardInterrupt + diff --git a/my.py b/my.py new file mode 100644 index 0000000..2b2334c --- /dev/null +++ b/my.py @@ -0,0 +1,12 @@ +import sys + +def processLine(line): + return processTokens(line.split()) + +def processTokens(tokens): + mysum = sum( [int(s) for s in tokens if s.isdigit()] ) + return mysum + +if __name__ == "__main__": + for line in sys.stdin: + print(processLine(line))