如何从 docker 容器访问外部文件?

时间:2021-07-16 10:45:10

标签: django docker docker-compose file-not-found

我使用 django 作为我的框架。 我们使用 docker 来管理服务器。

当我尝试跑步时:

docker-compose exec -T  web python3 manage.py shell < /data01/ipse/litework/second_level_controller/load.py

加载一些数据时出现以下错误:

<块引用>

文件“/code/second_level_controller/DataBaseLoader.py”,第 53 行,在 load_filenames 中 with open(os.path.join(self.data_directory, 'manifest.txt')) as f:

FileNotFoundError: [Errno 2] 没有这样的文件或目录: '/data01/ipse/rawdata/manifest.txt'

但是文件 /data01/ipse/rawdata/manifest.text 存在,我可以正常访问该文件:

nano /data01/ipse/rawdata/manifest.txt 

如何正确访问文件?

1 个答案:

答案 0 :(得分:1)

尝试为 /data01 添加卷挂载,以便这些文件位于 docker 映像内的相同位置。

https://docs.docker.com/storage/volumes/

https://docs.docker.com/compose/compose-file/compose-file-v3/#volume-configuration-reference

这看起来像:

services:
  webserver:
    image: python
    volumes:
       - /data01:/data01