如何从Python Docker镜像访问本地文件系统?

时间:2021-07-06 00:23:41

This might have an answer elsewhere but I could not find it.
I have a Docker image for Python 3.x:

这可能在其他地方有答案,但我找不到它。我有一个Python 3.x的Docker镜像:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python              latest              26acbad26a2c        2 months ago        690MB

I run the image using this command:

我使用这个命令运行图像:

$ docker run -i -t python

However, from inside the Docker container I want to be able to access my local filesystem to import Python modules etc.

但是,从Docker容器内部我希望能够访问我的本地文件系统以导入Python模块等。

How can I achieve this?

我怎样才能做到这一点?

2 个解决方案

#1


0  

you have to mount your local folder into container to

你必须将本地文件夹挂载到容器中

docker run -v /host/folder:/container/folder-i -t python

docker run -v / host / folder:/ container / folder -i -t python

#2


0  

The docker container's filesytem is isolated from the host's filesystem. Thus by default you cannot access files on the host unless you mount the directory onto the container.

docker容器的filesytem与主机的文件系统隔离。因此,默认情况下,除非将目录装载到容器上,否则无法访问主机上的文件。

To do that, you need to use a bindmount to mount the host directory onto the container:

为此,您需要使用bindmount将主机目录挂载到容器上:

docker run -it --volume <host-folder-path>:<container-folder-path> python

Now, Inside the container you will have the host folder accessible under "container-folder-path"

现在,在容器内部,您将在“container-folder-path”下访问主机文件夹

#1


0  

you have to mount your local folder into container to

你必须将本地文件夹挂载到容器中

docker run -v /host/folder:/container/folder-i -t python

docker run -v / host / folder:/ container / folder -i -t python

#2


0  

The docker container's filesytem is isolated from the host's filesystem. Thus by default you cannot access files on the host unless you mount the directory onto the container.

docker容器的filesytem与主机的文件系统隔离。因此,默认情况下,除非将目录装载到容器上,否则无法访问主机上的文件。

To do that, you need to use a bindmount to mount the host directory onto the container:

为此,您需要使用bindmount将主机目录挂载到容器上:

docker run -it --volume <host-folder-path>:<container-folder-path> python

Now, Inside the container you will have the host folder accessible under "container-folder-path"

现在,在容器内部,您将在“container-folder-path”下访问主机文件夹