从Windows访问Docker容器文件 [英] Accessing Docker container files from Windows

查看:239
本文介绍了从Windows访问Docker容器文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Windows文件资源管理器访问Docker容器?

解决方案

如果在Windows上运行Docker Desktop ,Docker容器不是在本地文件系统上本地运行,而是在hyper-v虚拟机上或通过WSL2运行。


Hyper-v(旧版)


从理论上讲,如果要停止hyper-v vm,则可以打开vhdx,并且如果具有正确的文件系统驱动程序,请挂载它并查看其中的文件。虚拟机运行时无法执行此操作。默认情况下,以Linux容器模式运行的OS名为 Docker Desktop,但运行busybox。


可以在此处找到文件:

  C:\ProgramData\DockerDesktop\vm-data\DockerDesktop.vhdx 


WSL2(现代)


WSL稍有不同,但差别不大。您仍然可以在虚拟环境上有效地工作。


然而,WSL的一大优点是,您实际上可以使用Windows资源管理器天真地浏览此文件系统。


通过浏览到\\wsl $,您将能够看到您拥有的所有发行版的文件系统,包括docker-desktop。


我的docker文件系统机器似乎生活在:

  \\wsl $ \docker-desktop-data\version-pack-data\community\ \docker\overlay2 

但是,覆盖了合并视图,该视图显示了您所做的更改的原始文件系统似乎无法通过Windows资源管理器工作,并为您提供了一个空白窗口。但是,您仍然可以看到'diff'文件夹,其中包含您所做的更改。


您可以使用powershell中的wsl命令为这两个实例之一打开终端。


通过Docker访问


如果您想看看此Docker OS和文件系统,一种方法是启动一个容器,该容器可以访问操作系统位于根目录上,

  docker run -it --mount type = bind,source = /,target = / host ubuntu / bin / bash 

这应该使您进入带有Bash终端的Ubuntu Docker容器,该容器的根目录为hyper- v容器(/),安装在路径 / host上。在内部,您将找到运行docker的虚拟机的Busybox文件系统以及所有容器。


由于docker的运行方式,您将能够访问每个Docker的文件系统容器。如果您对容器使用overlay2文件系统,则可能会在这里找到每个容器的文件系统层:

  / host / var / lib / docker / overlay2 

如果要在Windows资源管理器中浏览的文件,则应该能够配置samba导出


但是,如果目标是能够浏览/编辑本地OS上的文件,则可以从主机访问该文件夹,而在运行该容器时可以访问该文件夹。让它们在容器内更新,通常最简单的方法是将本地目录安装到容器中。可以与上面的示例类似地完成此操作,但是您首先需要进入Docker Desktop设置,并启用将共享驱动器安装到主机虚拟机中,然后在启动容器时提供volume参数。 / p>

如果您使用的是WSL2,则还有更多可用选项,因为您可以将项目保留在WSL层中,同时可以通过主机OS或通过docker与项目进行交互。最佳做法仍在不断变化,因此我将避免在此处给出直接建议。


How can I access Docker containers Folder and files from Windows file explorer?

解决方案

If you are running Docker Desktop on Windows, Docker containers don't run natively on the local filesystem, but instead on a hyper-v virtual machine or via WSL2.

Hyper-v (legacy)

In theory, if you were to stop the hyper-v vm, you could open up the vhdx, and if you had the right filesystem drivers, mount it and see the files inside. This is not possible to do while the virtual machine is running. By default the OS that runs for Linux container mode is named "Docker Desktop", but runs busybox.

The file could be found here:

C:\ProgramData\DockerDesktop\vm-data\DockerDesktop.vhdx

WSL2 (modern)

WSL things are slightly different, but not much. You are still effectively working with a virtual environment.

One of the nice advantages of WSL however, is that you can actually browse this file system naively with Windows Explorer.

By browsing to \\wsl$ you will be able to see the file systems of any distributions you have, including docker-desktop.

The docker filesystems on my machine seem to live in:

\\wsl$\docker-desktop-data\version-pack-data\community\docker\overlay2

However, the overlay 'merged' view, which shows the original file system with your changes, doesn't seem to work via windows explorer and gives you a blank window. You can however still see the 'diff' folder, which contains your changes.

You can open a terminal to either of these instances by using the wsl command, from powershell.

Access via Docker

If you wanted to have a look at this Docker OS and filesystem, one way would be to spin up a container, that has access to the OS at the root, something like:

docker run -it --mount type=bind,source=/,target=/host ubuntu /bin/bash

This should drop you into a Ubuntu docker container, with a Bash terminal, which has the root of the hyper-v container (/), mounted on the path '/host'. Looking inside, you will find the Busybox filesystem of the virtual machine that is running docker, and all the containers.

Due to how docker runs, you will be able to access the filesystems of each container. If you are using the overlay2 filesystem for you containers, you would likely find the filesystem layers here for each container:

/host/var/lib/docker/overlay2

If the files you want to browse through in windows explorer, you should be able to configure a samba export of this folder, that is accessible from the host machine, that is accessible while this container is running.

If the goal however is to be able to browse/edit files on the local OS, and have them update inside the container, normally the easiest way to do this, is to mount local directory into the container. This can be done similar to the example above, but you first need to go into the Docker Desktop settings, and enable the mounting of the shared drive into the host virtual machine, and then provide the volume argument when you spin up a container.

If you are using WSL2, there are a few more options available to you, as you can keep your projects inside the WSL layer, while interacting with them from the host OS or via docker. Best practice for this is still in flux, so I'm going to avoid giving direct advice here.

这篇关于从Windows访问Docker容器文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆