[英]Docker Running Python Error - [Error 2] No such file or directory
I am new to docker, I have prepared a dockerfile and built a basic docker image, however, I got this error when I run it.我是 docker 的新手,我准备了一个 dockerfile 并构建了一个基本的 docker 映像,但是,当我运行它时出现此错误。 My dockerfile is below:我的 dockerfile 如下:
FROM python:3.8.6
MAINTAINER tester tester
ADD . /code
WORKDIR /code
CMD ["python", "/app/Main.py"]
I used this command to build my docker image (I am in folder "test" to execute this command):我使用此命令构建了我的 docker 映像(我在文件夹“test”中执行此命令):
docker build -t my_test .
And then I use this command tp run my docker image:然后我使用这个命令 tp 运行我的 docker 图像:
docker run -it my_test
Then I have got this following error:然后我收到以下错误:
python: can't open file '/app/Main.py': [Errno 2] No such file or directory
I can confirm the folder "app" has got script "Main.py".我可以确认文件夹“app”有脚本“Main.py”。 What is the possible reason?可能的原因是什么?
You're using a hard coded file path in the CMD
which is ignoring your working directory, remove the prefixing slash您在忽略您的工作目录的CMD
中使用硬编码文件路径,删除前缀斜杠
CMD ["python", "app/Main.py"]
When you start a path with a /
it corresponds to the root level.当您以/
开始路径时,它对应于根级别。 Change the following line as follows:如下更改以下行:
CMD ["python", "/app/Main.py"]
to:至:
CMD ["python", "app/Main.py"]
OR或者
CMD ["python", "./app/Main.py"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.