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

Advertisements

I have a python file – script.py

import os

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

And a docker file

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

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>

Output:

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