-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |