Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Docker container files from Windows

Tags:

docker

windows

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

like image 758
Nafis Avatar asked Nov 21 '15 21:11

Nafis


People also ask

How do I view Docker files in Windows?

By browsing to \\wsl$ you will be able to see the file systems of any distributions you have, including docker-desktop. 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.

Where are Docker container files stored Windows?

If you want to access the image data directly, it's usually stored in the following locations: Linux: /var/lib/docker/ Windows: C:ProgramDataDockerDesktop. macOS: ~/Library/Containers/com.


Video Answer


2 Answers

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.

like image 62
KHobbits Avatar answered Oct 19 '22 09:10

KHobbits


Another related question's reply answers this: https://stackoverflow.com/a/64418064/1115220

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

like image 28
JasonS Avatar answered Oct 19 '22 10:10

JasonS