【问题标题】:How to make the docker container search the file within the host rather than within the container?如何让 docker 容器在主机内而不是容器内搜索文件?
【发布时间】:2020-11-20 12:43:57
【问题描述】:

我有一个 python 脚本,它从用户处理该文件获取输入文件路径,并在终端中用户指定的位置提供输出文件。下面是我的代码,它采用文件路径。

inputfilepath = input("Enter the input file path")
print(inputfilepath)
outputfilepath = input("Enter the output file path")
print(outputfilepath)
inputPath = inputfilepath

如果我在 Pycharm 中运行此 python 代码,它可以正常工作,但如果我创建相同代码的 docker 映像并输入输入文件路径,它会给出“找不到文件错误”。

Providing the input and output file path to the running container

This is the error message

因为如果我给出主机/本地机器的文件路径,它会在容器的工作目录中搜索该路径,而不是在本地机器中

如何让容器搜索本地文件系统上的文件路径? 提前致谢!

【问题讨论】:

    标签: python docker containers


    【解决方案1】:

    您应该将文件(或包含文件的目录)从主机挂载到容器。

    假设您希望容器中的路径与主机中的路径相同:

    docker run \
      --mount type=bind,source=/path/to/directory,target=/path/to/directory
    

    请参阅documentation 关于绑定挂载。

    【讨论】:

    • 非常感谢!你拯救了我的一天。我最后使用了您的命令以及 -it 和图像名称,它起作用了。 docker run -it --mount type=bind,source=/home/poorna/inputfiles,target=/app/files imagename.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 2020-11-10
    • 2015-11-11
    • 2016-09-30
    • 1970-01-01
    相关资源
    最近更新 更多