How to run a python script that takes a file as input, opens it and prints the content inside a docker container


I have a python file –

import os

filename = os.getenv("filename")
with open(filename) as f:
    message = f.readlines()

And a docker file

FROM python:3.9
COPY . /app/
ENTRYPOINT ["python3", ""]

I created the docker image. Now I am not able to run the container successfully. I tried specifying the volume but not working.

This is the command I tried:

docker run --volume=<path to folder where I have a text file> --env filename=<path to text file> <docker image name>


No such file or directory:

>Solution :

Your –volume will simply create a new volume mounted as your path. You’ll need to map your local directory to somewhere and then use that as part of the path you’re passing to the container. Something like:

docker run --volume=/tmp/test:/tmp --env filename=/tmp/test.txt container_name

Leave a Reply Cancel reply