custon docker python image中的FileNotFoundError [英] FileNotFoundError in custon docker python image
问题描述
我有一个python代码,我在其中打开一个文件,然后提取特定的数据.下面是该代码:
I have a python code in which I am opening a file and then extracting a particular data. Below is that code:
def get_mode(first, last):
with open('/srv/config/mode.conf','r') as f:
for line in f:
if line.startswith(first):
try:
start = line.index(first) + len(first)
end = line.index(last, start)
return line[start:end]
except ValueError:
return "Default Mode"
当我执行python代码时,这可以正常工作.但是我想将其作为docker运行,因此我已使用以下命令将其转换为docker映像:
This works fine when I execute the python code. But I want to run it as a docker so I have converted it into docker image using below command:
sudo docker build -t mydocker .
以上命令已成功将python代码转换为docker映像.然后我使用
Above command successfully convert the python code into docker image. Then I run the image using
sudo docker run -it mydocker
但是它给出了错误:
Traceback (most recent call last):
File "./my_script.py", line 68, in <module>
main()
File "./my_script.py", line 52, in main
mode = get_mode("user "," :")
File "./my_script.py", line 15, in get_publish_string
with open('/srv/config/mode.conf','r') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/srv/config/mode.conf'
如果python代码运行良好,那么为什么docker image给出错误呢?
How is that possible that if the python code is running fine then why docker image is giving error.
DOCKERFILE的内容:
CONTENT OF DOCKERFILE:
FROM python:3
ADD my_script.py /
ADD mode.conf /srv/config
RUN pip3 install psutil
CMD [ "python3", "./my_script.py" ]
推荐答案
您还需要将配置文件添加到映像中.
You need to add the config file to your image as well.
RUN mkdir -p /srv/config
ADD mode.conf /srv/config
或类似的东西.
这篇关于custon docker python image中的FileNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!