简体   繁体   English

自定义 docker python 图像中的 FileNotFoundError

[英]FileNotFoundError in custon docker python image

I have a python code in which I am opening a file and then extracting a particular data.我有一个 python 代码,我在其中打开一个文件,然后提取特定数据。 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"

This works fine when I execute the python code.当我执行 python 代码时,这工作正常。 But I want to run it as a docker so I have converted it into docker image using below command:但我想将它作为 docker 运行,所以我使用以下命令将其转换为 docker 图像:

sudo docker build -t mydocker .

Above command successfully convert the python code into docker image.以上命令成功将 python 代码转换为 docker 图像。 Then I run the image using然后我使用运行图像

sudo docker run -it mydocker

but it gives error:但它给出了错误:

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'

How is that possible that if the python code is running fine then why docker image is giving error.如果 python 代码运行良好,那么为什么 docker 图像会出错。

CONTENT OF DOCKERFILE: 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

or something like that. 或类似的东西。

If you installed docker-compose by downloading a binary with curl, try removing it如果您通过下载带有 curl 的二进制文件来安装 docker-compose,请尝试将其删除

rm /usr/local/bin/docker-compose #replace the path with yours if it doesn't match

Then install docker-compose with apt然后用apt安装docker-compose

sudo apt install docker-compose

Reboot重启

sudo reboot

And try launching并尝试启动

docker-compose -v

to verify that everything is working well验证一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM