Accessing Docker container files from Windows

后端 未结 2 1014
刺人心
刺人心 2021-02-06 23:37

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

相关标签:
2条回答
  • 2021-02-07 00:11

    You should use a mount volume. In your docker run .... command, you may specify a mount volume. The syntax is as follows:

    -v /host/directory:/container/directory

    An example:

    docker run -it -v C:\Users\thomas\Desktop:/root/home --name my_container image1

    This would allow the container to write files to /root/home and have them appear on the user thomas' desktop

    0 讨论(0)
  • 2021-02-07 00:34

    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.

    0 讨论(0)
提交回复
热议问题