custon docker python映像中的FileNotFoundError

custon docker python映像中的FileNotFoundError,python,docker,Python,Docker,我有一个python代码,在其中打开一个文件,然后提取一个特定的数据。下面是代码: 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) + l

我有一个python代码,在其中打开一个文件,然后提取一个特定的数据。下面是代码:

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图像:

sudo docker build -t mydocker .
上面的命令成功地将python代码转换为docker映像。然后我使用

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'

您还需要将配置文件添加到映像中

RUN mkdir -p /srv/config
ADD mode.conf /srv/config

或者类似的

请发布Dockerfile的内容。您需要将配置文件添加到docker容器中或以其他方式装载它。它不会自动可用:)@favoretti我已经添加了Dockerfile的内容。你能告诉我如何在Dockerfile中提到配置文件吗。谢谢。它给出了错误
添加失败:stat/var/lib/docker/tmp/docker-builder771094008/mode.conf:没有这样的文件或目录
我用dockerfile的内容更新了我的问题你的mode.conf和dockerfile在同一个目录下吗?我还修改了我的答案,加入了mkdir,以防目录不存在。mode.conf位于
/srv/config/
中。它不在同一目录中。我是否还需要将其复制到Dockerfile所在的my current目录中。另外,如果我包含
runmkdir-p/srv/config
,我想它会创建一个目录,但该目录已经存在。若路径已经存在于您的图像上,那个么您不需要mkdir,但它也不会造成任何伤害。
RUN mkdir -p /srv/config
ADD mode.conf /srv/config