Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 – 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading