英文:
How to run a python script that takes a file as input, opens it and prints the content inside a docker container
问题
以下是翻译好的部分:
我有一个 Python 文件 - script.py
import os
filename = os.getenv("filename")
with open(filename) as f:
message = f.readlines()
print(message)
还有一个 Docker 文件
FROM python:3.9
WORKDIR /app
COPY . /app/
ENTRYPOINT ["python3", "script.py"]
我创建了 Docker 镜像。现在我无法成功运行容器。我尝试指定卷但不起作用。
这是我尝试的命令:
docker run --volume=<路径到包含文本文件的文件夹> --env filename=<文本文件的路径> <Docker 镜像名称>
输出:
没有这样的文件或目录:
英文:
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:
答案1
得分: 1
--volume 选项将简单地创建一个作为您路径挂载的新卷。您需要将本地目录映射到某个位置,然后将其作为您传递给容器的路径的一部分使用。类似于:
docker run --volume=/tmp/test:/tmp --env filename=/tmp/test.txt container_name
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论