Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
miladhub committed Jan 19, 2019
1 parent eeb8a5e commit 842ac96
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM python:3.6-alpine

RUN mkdir /app
COPY my.py /app
WORKDIR /app

ENTRYPOINT ["python3", "-u", "my.py"]

52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <module>
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 <module>
for line in sys.stdin:
KeyboardInterrupt

12 changes: 12 additions & 0 deletions my.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 842ac96

Please sign in to comment.