Pawel Wisniewski
Pawel Wisniewski

Reputation: 440

File as command line argument of Docker python app

I need to create dockerized python application that will read file from command line.

I don't know if it's possible, but my specification for running this app is:

docker run -t myimage file.csv

At the moment my docker file look like this:

FROM python:3
ADD test.py /
ENTRYPOINT ["python", "test.py"]
CMD ["test.py"]

Where test.py is: import sys

def main(args):
    for a in args:
        print(a)
        with open(a, 'r') as f:
            for l in f:
                print(l)


if __name__ == '__main__':
    main(sys.argv)

Upvotes: 0

Views: 1360

Answers (1)

BMitch
BMitch

Reputation: 263726

The docker container won't see the file unless you map it into the container as a volume. That syntax would look like:

docker run -t -v `pwd`/file.csv:/file.csv myimage file.csv

That maps ./file.csv into the container as /file.csv, and then your script in the container will run:

python test.py file.csv

You can also use the docker cli to pipe input into the container, replacing stdin. From the CLI, you need to include -i to make it interactive, and then use the normal shell redirection like:

docker run -it myimage <file.csv

If you do this, you'd need to update your python script to read from stdin instead of the file, and you wouldn't have the option to be interactive with the python script if you were using stdin for other things.

Upvotes: 2

Related Questions