Mount a Host Folder Without Losing Data
Share a folder from your computer with a container, make it read-only and see how a mount can hide files that were already there. After this, you can mount a folder and know what it will and will not keep.
A bind mount shows a folder from your computer inside a container. What the container writes there lands on your disk, so it is still there after the container is gone. Used well, it keeps your data safe. Used carelessly, it hides files or lets a container change things you wanted left alone. This page shows both sides.
The command
Create a folder, mount it, and write from inside the container. ${PWD} is the current folder in PowerShell. In cmd.exe write %cd%, and on Linux $(pwd).
New-Item -ItemType Directory -Force -Name data | Out-Null
docker run --rm --name mount-demo -v "${PWD}/data:/data" alpine:3.24 sh -c "echo written by the container >> /data/log.txt"
Get-Content .\data\log.txt
written by the container
The part after -v has the host folder, a colon, and the folder inside the container. The container is gone because of --rm, and the file is still on your computer. Run it again and the file gets a second line, because >> appends.
docker run --rm -v "${PWD}/data:/data" alpine:3.24 sh -c "echo second run >> /data/log.txt"
Get-Content .\data\log.txt
written by the container
second run
That is the point of a mount: the data belongs to the folder, not to the container.
Make it read-only
When a container only needs to read your files, add :ro at the end. A write then fails, which is what you want.
docker run --rm -v "${PWD}/data:/data:ro" alpine:3.24 sh -c "echo oops >> /data/log.txt"
sh: can't create /data/log.txt: Read-only file system
Use :ro for settings files and website content. A bug or a bad command in the container then cannot damage the original.
The mount hides what was there
A bind mount replaces the container's folder with yours. If the image had files there, you stop seeing them. Mount an empty folder over /etc/apk and compare.
New-Item -ItemType Directory -Force -Name empty | Out-Null
docker run --rm alpine:3.24 ls /etc/apk
docker run --rm -v "${PWD}/empty:/etc/apk" alpine:3.24 ls /etc/apk
keys
repositories
The first command lists the files that come with the image, five of them in my run. The second prints nothing, because your empty folder is now what the container sees. The image's files are not deleted, only hidden while the mount is there. If an application fails after you add a mount, this is the first thing to check. Mount a folder that has the files the program needs, or a subfolder that the image does not use.
A mistyped path
Say you mean to mount data and you type dat. Docker does not complain. It creates the folder you misspelled and mounts that, so the container starts with an empty folder and your real data looks gone.
Test-Path .\misspelled
docker run --rm -v "${PWD}/misspelled:/data" alpine:3.24 ls -A /data
Test-Path .\misspelled
False
True
The folder did not exist before the run and does now, and ls printed nothing because it is empty. I tried --mount as well and Docker Desktop on Windows created the folder the same way. Docker's documentation says --mount refuses a missing path on a Linux engine, so the behaviour depends on where it runs. The habit that works everywhere: when a mounted folder is empty, look at the path in the command before you look anywhere else.
Bind mount or volume?
Use a bind mount when you want to see the files on your computer: source code, a config file, a folder of uploads. Use a named volume (docker volume create) when the data is for Docker only, such as a database. Volumes are faster on Windows, because the files stay inside the Linux side instead of crossing to the Windows file system.
💡 Tip: On Linux, files a container creates in a bind mount belong to the user the container runs as, which is often root. You may need sudo to remove them. Docker Desktop on Windows hides this.
âš Warning: Never mount your whole user folder or the Docker socket into a container you do not trust. The container can then read, change or delete everything in it.
Related course: Docker for Beginners.
About
Share a folder from your computer with a container, make it read-only and see how a mount can hide files that were already there. After this, you can mount a folder and know what it will and will not keep.
- Responsible
- John G
- Last Update
- 08/06/2026
- Reading Time
- 4 min
Reviews
Sign in to write a reviewComments (0)
Sign in to commentNo comments yet.