简体   繁体   English

使用python从docker容器访问主机os文件

[英]accessing host os file from docker container using python

I want to capture the content of file which is located in the host os (linus) into a variable from the Docker container using a python script 我想使用python脚本从Docker容器中将位于主机os(linus)中的文件内容捕获到一个变量中

Can I do this 我可以这样做吗

FILE="/home/test/file.txt"
#open the file for read-only
fd = os.open(FILE,os.O_RDONLY)
content = os.read(fd,12)
print content

ended up with this error 最终出现此错误

OSError: [Errno 2] No such file or directory: '/home/test/file.txt'

Please suggest a way if I am doing it wrong 如果我做错了,请提出一种方法

mount the volume where the file is located into a directory in your docker container 将文件所在的卷挂载到Docker容器的目录中

you do this by using the -v flag in docker 您可以通过在docker中使用-v标志来执行此操作

for example: 例如:

docker run -v <HOST_FOLDER>:/data <IMAGE>

that will mount to the folder data on the root of your container ( /data ) 它将挂载到容器根目录( /data )上的文件夹data

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

相关问题 使用 vscode 从主机调试 docker 容器中运行的 python 代码 - debug python code running in docker container from host using vscode Docker容器中的Python服务器脚本不接收来自主机操作系统的请求 - Python server script in a Docker container doesn't receive requests from host OS Docker | 访问 docker 容器内 python 中的主机文件 - Docker | access host file in python within docker container 如何使用 docker-py(docker SDK)将文件从主机复制到容器 - How to copy a file from host to container using docker-py (docker SDK) 使用python脚本将文件从docker复制到主机系统 - copy file from docker to host system using python script 从 Python 程序的 docker 容器中获取主机名 - Get the host name from within the docker container from Python program 如何使用docker-py中的副本将文件从容器复制到主机 - How to copy a file from container to host using copy in docker-py 如何使用Python在Docker容器中获取主机ip? - How can I get the host ip in a docker container using Python? 尝试从主机连接到docker容器内的python套接字 - Trying to connect to a python socket inside a docker container from host python docker如何将目录从主机挂载到容器 - python docker how to mount the directory from host to container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM