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

huangapple go评论76阅读模式
英文:

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(&quot;filename&quot;)
with open(filename) as f:
    message = f.readlines()
    print(message)

And a docker file

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

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=&lt;path to folder where I have a text file&gt; --env filename=&lt;path to text file&gt; &lt;docker image name&gt;

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

huangapple
  • 本文由 发表于 2023年2月23日 22:22:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定