In part 3 you ran hello-app, stopped it and removed it. Nothing it did was worth keeping. Real programs write files: a database, uploads, a counter. This part gives hello-app a visit counter stored in a file, shows the count disappear when the container is removed, and then keeps it two ways: in a named volume that Docker manages, and in a bind mount of a folder you choose. By the end you can look at any container and say which kind of storage it uses.
Time and setup
- Reading: about 13 minutes.
- Optional lab: about 20 minutes.
- Optional advanced step: about 10 minutes.
You need the hello folder and the hello-app source from part 2, and curl. The series uses Docker Engine 29.10.1 and Compose v5.8.1, and writes docker without sudo. The file owners and paths in this lab come from Docker Engine on Linux with its daemon running as root; on Docker Desktop, or in rootless mode, files can show up as yours, and volumes live inside Docker’s own virtual machine or data folder.
What you will build
hello-app:1.2, which answers Hello from container ..., visit 3 and keeps the count in /data/visits.txt. You will run it three ways: with no mount, with the named volume hello-data, and with your own data folder bind-mounted at /data.
Why the count disappears
Part 2 showed that each container gets a thin writable layer on top of the image’s read-only layers. Docker’s storage overview is direct about what that means: “Data written to the container layer doesn’t persist when the container is destroyed.” docker stop keeps the layer; docker rm deletes it. A new container from the same image starts with an empty writable layer again.
To keep data, you mount storage from outside the container at a path inside it. The program writes to /data as usual; the mount decides where the bytes really go. The overview lists several mount types. This part uses the two you will meet most:
- Volumes are “persistent storage mechanisms managed by the Docker daemon. They retain data even after the containers using them are removed.”
- Bind mounts “create a direct link between a host system path and a container, allowing access to files or directories stored anywhere on the host.”
Named volumes
A named volume is a directory that Docker creates and tracks. You create one with docker volume create hello-data, or Docker creates it the first time a container asks for it. The volumes page explains the lifecycle: “When a container is destroyed, the writable layer is destroyed with it. Using a volume ensures that the data is persisted even if the container using it is removed.” Removing a container never removes a named volume; you do that yourself with docker volume rm.
On Linux the files sit under /var/lib/docker/volumes/, but you should treat that path as Docker’s. The storage overview warns: “Directly accessing or interacting with the volume data is unsupported, undefined behavior, and may result in the volume or its data breaking in unexpected ways.” To read a volume, mount it into a container.
The volumes page adds two more details. If you mount an empty volume over a directory that already has files in the image, Docker copies those files into the volume first. And a volume can also be anonymous: it gets a random name instead of one you chose, and is not shared between containers automatically. Removing a container leaves its anonymous volumes behind, unless the container ran with --rm. The PostgreSQL image, for example, declares a volume at /var/lib/postgresql, so a container started from it without a mount there gets an anonymous one.
Bind mounts
A bind mount takes a path on the host, usually a folder, and makes it appear at a path in the container. Both sides see the same files at the same time. The bind mounts page lists the use cases: sharing source code with a container, keeping files a container generates on the host, and sharing configuration files. Docker itself bind-mounts /etc/resolv.conf into containers so they can resolve names.
That directness has costs the same page spells out. “Bind mounts have write access to files on the host by default”, so a process in the container can change or delete host files. And containers with bind mounts “may fail if run on a different host without the same directory structure.” Add readonly when the container only needs to read. Mount over a directory that already has files in the image, and those files are hidden for as long as the mount is there.
Files in a bind mount also keep their owners as numbers. hello-app runs as root, user ID 0, so a file it creates shows up on your host as owned by root. The advanced step shows the effect and the usual fix, --user.
–mount and -v
There are two ways to write a mount on docker run. The long form names every part:
--mount type=volume,src=hello-data,dst=/data
--mount type=bind,src="$PWD"/data,dst=/data
The short form packs them into one value separated by colons: -v hello-data:/data for a volume, -v "$PWD"/data:/data for a bind mount. -v decides which type you meant from the first part: a name means a volume, a path means a bind mount. The bind mounts page says “In general, –mount is preferred”, because it is explicit and supports every option. They also behave differently when the host path does not exist: -v creates it, always as a directory, while --mount stops with an error. You will see both in the lab. This series uses --mount with docker run; Compose files use a short form of their own.
Optional lab: keep the count
This lab is optional. Every command ran on 30 November 2026 with Docker Engine 29.10.1 in /home/ubuntu/hello; your IDs, paths and times will differ.
Safety: this lab deletes data it creates. It makes and then removes a volume named
hello-data, and folders nameddataandmissinginside yourhellofolder. If you already have a volume calledhello-data(check withdocker volume ls) or adatafolder there that matters to you, rename them in the commands below, or the cleanup step will delete them. It also publishes port 8080 on 127.0.0.1; if that port is busy, Docker refuses to start the container and nothing else is affected. Other containers, volumes and images keep running and stay untouched. To undo everything, rundocker rm -f hello, thendocker volume rm hello-data, thenrm -rf dataandrmdir missingin thehellofolder.
1. Teach hello-app to count. In the hello folder, open app.py. Under the line PORT = 8000, add the two settings and the function:
DATA_DIR = os.environ.get("DATA_DIR", "/data")
COUNT_FILE = os.path.join(DATA_DIR, "visits.txt")
def count_visit():
os.makedirs(DATA_DIR, exist_ok=True)
try:
with open(COUNT_FILE) as f:
visits = int(f.read().strip() or 0)
except FileNotFoundError:
visits = 0
visits += 1
with open(COUNT_FILE, "w") as f:
f.write(f"{visits}\n")
return visits
Then change the first line of do_GET so it counts the visit and reports it:
def do_GET(self):
visits = count_visit()
body = f"{GREETING} from container {socket.gethostname()}, visit {visits}\n".encode()
The Dockerfile stays the same. Build the new version:
docker build -t hello-app:1.2 .Explain this command
docker build -t hello-app:1.2 .docker buildBuild an image from the Dockerfile in a folder.-t hello-app:1.2Tag the result with the name hello-app and the tag 1.2..Use the current folder as the build context.
...
#8 naming to docker.io/library/hello-app:1.2 done
#8 unpacking to docker.io/library/hello-app:1.2 done
#8 DONE 0.0s
2. Count without a mount, and lose it.
docker run -d --name hello -p 127.0.0.1:8080:8000 hello-app:1.2
curl http://127.0.0.1:8080/
curl http://127.0.0.1:8080/
docker rm -f hello
docker run -d --name hello -p 127.0.0.1:8080:8000 hello-app:1.2
curl http://127.0.0.1:8080/
docker rm -f helloExplain this command
docker run -d --name hello -p 127.0.0.1:8080:8000 hello-app:1.2
curl http://127.0.0.1:8080/
curl http://127.0.0.1:8080/
docker rm -f hello
docker run -d --name hello -p 127.0.0.1:8080:8000 hello-app:1.2
curl http://127.0.0.1:8080/
docker rm -f hellodocker run -d --name helloRun hello-app in the background, named hello.-p 127.0.0.1:8080:8000Publish container port 8000 on host port 8080, this machine only.hello-app:1.2The new image that counts visits in /data/visits.txt.curl http://127.0.0.1:8080/Ask for a page. Each request adds one visit.docker rm -f helloStop and remove the container, with its writable layer.
2d3068c5a15f54c1db9e1f7b98bb6b6d33b11c689ec68759f884b1c5e0eb8e82
Hello from container 2d3068c5a15f, visit 1
Hello from container 2d3068c5a15f, visit 2
hello
26989c7242662d65a49d545f4b44db1bc26ca37f52f5d034f2bb31e875b91f83
Hello from container 26989c724266, visit 1
hello
The count reached 2, then the new container started again at 1. The file was in the first container’s writable layer, and docker rm deleted it.
3. Keep it in a named volume.
docker volume create hello-data
docker run -d --name hello -p 127.0.0.1:8080:8000 --mount type=volume,src=hello-data,dst=/data hello-app:1.2
curl http://127.0.0.1:8080/
curl http://127.0.0.1:8080/
docker rm -f hello
docker run -d --name hello -p 127.0.0.1:8080:8000 --mount type=volume,src=hello-data,dst=/data hello-app:1.2
curl http://127.0.0.1:8080/Explain this command
docker volume create hello-data
docker run -d --name hello -p 127.0.0.1:8080:8000 --mount type=volume,src=hello-data,dst=/data hello-app:1.2
curl http://127.0.0.1:8080/
curl http://127.0.0.1:8080/
docker rm -f hello
docker run -d --name hello -p 127.0.0.1:8080:8000 --mount type=volume,src=hello-data,dst=/data hello-app:1.2
curl http://127.0.0.1:8080/docker volume create hello-dataCreate a named volume called hello-data.--mount type=volume,src=hello-data,dst=/dataMount the volume hello-data at /data inside the container.curl http://127.0.0.1:8080/Count a visit. The count is written into the volume.docker rm -f helloRemove the container. The volume stays.
hello-data
dd8c9a2200f735219aff83de94a9d73d913bb96396d400e51de49e8c3d2689fb
Hello from container dd8c9a2200f7, visit 1
Hello from container dd8c9a2200f7, visit 2
hello
1ef8d9e75cd06f8bb603a0f216b59812d92b93cf52f4e85816b2de00fea26a99
Hello from container 1ef8d9e75cd0, visit 3
A different container ID, and the count continued at 3. The file lived in hello-data, which docker rm -f left alone. Leave this container running for the next step.
4. Say which mount it uses.
docker volume ls
docker volume inspect hello-data
docker inspect --format '{{range .Mounts}}{{.Type}} {{.Name}} {{.Source}} -> {{.Destination}}{{end}}' hello
docker rm -f helloExplain this command
docker volume ls
docker volume inspect hello-data
docker inspect --format '{{range .Mounts}}{{.Type}} {{.Name}} {{.Source}} -> {{.Destination}}{{end}}' hello
docker rm -f hellodocker volume lsList volumes with their driver and name.docker volume inspect hello-dataShow a volume's details as JSON, including where Docker keeps it.docker inspectShow low-level details of a container as JSON.--formatPrint only what the Go template asks for, here each mount's type, name, source and destination.docker rm -f helloRemove the container. The volume stays.
DRIVER VOLUME NAME
local hello-data
[
{
"CreatedAt": "2026-11-30T07:51:34Z",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/hello-data/_data",
"Name": "hello-data",
"Options": null,
"Scope": "local"
}
]
volume hello-data /var/lib/docker/volumes/hello-data/_data -> /data
hello
docker volume ls lists volumes; you may see others from earlier experiments. local is the built-in driver that stores the volume on this machine. The last line answers the question this part set out to answer: the container’s /data is a mount of Type volume, named hello-data. docker inspect reads it from the container itself, so it works on any container, including ones someone else started.
5. Share a folder with a bind mount. Make a data folder, start the count at 41 from the host, and mount the folder:
mkdir data
echo 41 > data/visits.txt
docker run -d --name hello -p 127.0.0.1:8080:8000 --mount type=bind,src="$PWD"/data,dst=/data hello-app:1.2
curl http://127.0.0.1:8080/
cat data/visits.txt
echo 99 > data/visits.txt
curl http://127.0.0.1:8080/
ls -l data
docker inspect --format '{{range .Mounts}}{{.Type}} {{.Source}} -> {{.Destination}}{{end}}' hello
docker rm -f helloExplain this command
mkdir data
echo 41 > data/visits.txt
docker run -d --name hello -p 127.0.0.1:8080:8000 --mount type=bind,src="$PWD"/data,dst=/data hello-app:1.2
curl http://127.0.0.1:8080/
cat data/visits.txt
echo 99 > data/visits.txt
curl http://127.0.0.1:8080/
ls -l data
docker inspect --format '{{range .Mounts}}{{.Type}} {{.Source}} -> {{.Destination}}{{end}}' hello
docker rm -f hellomkdir dataMake a folder named data in the current folder.echo 41 > data/visits.txtWrite the number 41 into the count file from the host.--mount type=bind,src="$PWD"/data,dst=/dataMount your data folder at /data in the container. $PWD is the current folder, so the source is an absolute path.cat data/visits.txtRead the count file from the host.echo 99 > data/visits.txtChange the count from the host while the container runs.ls -l dataList the folder with owners and permissions.docker inspect --formatPrint the container's mounts. This one says bind.docker rm -f helloRemove the container. Your folder stays.
39f4628d2de4eea5716a5f7a47cbcecaf86a7e0b1a8456a747caaee9c3daac03
Hello from container 39f4628d2de4, visit 42
42
Hello from container 39f4628d2de4, visit 100
total 4
-rw-rw-r-- 1 ubuntu ubuntu 4 Nov 30 07:51 visits.txt
bind /home/ubuntu/hello/data -> /data
hello
The container read the 41 you wrote and answered 42. You read its 42 with cat, wrote 99, and it answered 100. Both sides edit one file. This time docker inspect says bind, and the source is your folder. The file still belongs to you because you created it; the advanced step shows what happens when the container creates it.
6. A path that does not exist. Try both forms with a folder named missing that you have not created:
docker run -d --name hello --mount type=bind,src="$PWD"/missing,dst=/data hello-app:1.2
docker run -d --name hello -v "$PWD"/missing:/data hello-app:1.2
ls -ld missing
docker rm -f hello
rmdir missingExplain this command
docker run -d --name hello --mount type=bind,src="$PWD"/missing,dst=/data hello-app:1.2
docker run -d --name hello -v "$PWD"/missing:/data hello-app:1.2
ls -ld missing
docker rm -f hello
rmdir missing--mount type=bind,src="$PWD"/missing,dst=/dataAsk for a bind mount of a folder that does not exist. --mount refuses.-v "$PWD"/missing:/dataThe same mount in the short form. -v creates the missing folder on the host.ls -ld missingShow the folder itself, with its owner.docker rm -f helloRemove the container.rmdir missingRemove the empty folder.
docker: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /home/ubuntu/hello/missing
Run 'docker run --help' for more information
b7b0057b389873461a0d9aa27dd4f21207c4159c3e6d6992360c5d9ad0f22b9f
drwxr-xr-x 2 root root 4096 Nov 30 07:51 missing
hello
--mount refused, which is what you want when the path has a typo. -v quietly created missing, owned by root because the Docker daemon made it. You can still remove it with rmdir, since it is empty and sits in your own folder.
7. Clean up. This deletes the volume and its count. Removing a volume is permanent.
docker volume rm hello-data
docker volume ls --filter name=hello-data
rm -r dataExplain this command
docker volume rm hello-data
docker volume ls --filter name=hello-data
rm -r datadocker volume rm hello-dataDelete the volume and everything in it. This cannot be undone.docker volume ls --filter name=hello-dataCheck that the volume is gone.rm -r dataDelete the data folder in the current folder.
hello-data
DRIVER VOLUME NAME
The list is empty. If docker volume rm says volume is in use, a container still mounts it; remove that container first.
Optional advanced: who owns the files
Run the bind mount again, but let the container create the file. Then run it as your own user:
mkdir data
docker run -d --name hello -p 127.0.0.1:8080:8000 --mount type=bind,src="$PWD"/data,dst=/data hello-app:1.2
curl http://127.0.0.1:8080/
ls -l data
echo 5 > data/visits.txt
docker rm -f hello
rm -f data/visits.txt
docker run -d --name hello -p 127.0.0.1:8080:8000 --user "$(id -u):$(id -g)" --mount type=bind,src="$PWD"/data,dst=/data hello-app:1.2
curl http://127.0.0.1:8080/
ls -l data
echo 5 > data/visits.txt
curl http://127.0.0.1:8080/
docker rm -f helloExplain this command
mkdir data
docker run -d --name hello -p 127.0.0.1:8080:8000 --mount type=bind,src="$PWD"/data,dst=/data hello-app:1.2
curl http://127.0.0.1:8080/
ls -l data
echo 5 > data/visits.txt
docker rm -f hello
rm -f data/visits.txt
docker run -d --name hello -p 127.0.0.1:8080:8000 --user "$(id -u):$(id -g)" --mount type=bind,src="$PWD"/data,dst=/data hello-app:1.2
curl http://127.0.0.1:8080/
ls -l data
echo 5 > data/visits.txt
curl http://127.0.0.1:8080/
docker rm -f hello--mount type=bind,src="$PWD"/data,dst=/dataBind-mount the empty data folder. The app creates visits.txt itself.ls -l dataShow who owns the file the container created.echo 5 > data/visits.txtTry to write the file as your user.rm -f data/visits.txtDelete it without a question. That needs write permission on the folder, which you have.--user "$(id -u):$(id -g)"Run the app with your user ID and group ID instead of root.curl http://127.0.0.1:8080/Count a visit and read the answer.docker rm -f helloRemove the container.
f97435a236b7a268b13e5fbe590e00090d78da1c055bd447ccec6b78fb2ffc70
Hello from container f97435a236b7, visit 1
total 4
-rw-r--r-- 1 root root 2 Nov 30 07:51 visits.txt
bash: data/visits.txt: Permission denied
hello
e31be1158bfc77db8f24e0d17b7b1a2dc2c357e56d5bb3a811ac8c96ec19b890
Hello from container e31be1158bfc, visit 1
total 4
-rw-r--r-- 1 ubuntu ubuntu 2 Nov 30 07:51 visits.txt
Hello from container e31be1158bfc, visit 6
hello
The first file belonged to root, so your shell could not write it. rm -f still worked, because deleting a file needs write permission on the folder, which is yours; without -f, GNU rm stops to ask before it removes a write-protected file when you run it from a terminal. With --user, which the run reference describes as “Username or UID (format: <name|uid>[:<group|gid>])”, the app ran as your user and group IDs, 1000:1000 here, and the file came out as yours.
Last, mount the folder read-only and ask for a page:
docker run -d --name hello -p 127.0.0.1:8080:8000 --mount type=bind,src="$PWD"/data,dst=/data,readonly hello-app:1.2
curl http://127.0.0.1:8080/
docker logs -n 3 hello
docker rm -f hello
rm -r dataExplain this command
docker run -d --name hello -p 127.0.0.1:8080:8000 --mount type=bind,src="$PWD"/data,dst=/data,readonly hello-app:1.2
curl http://127.0.0.1:8080/
docker logs -n 3 hello
docker rm -f hello
rm -r data--mount type=bind,src="$PWD"/data,dst=/data,readonlyBind-mount the folder read-only. The container can read it but not write it.curl http://127.0.0.1:8080/Ask for a page. The app fails to write its count.docker logs -n 3 helloShow the last three log lines, with the error.docker rm -f helloRemove the container.rm -r dataDelete the data folder.
794f160123c0a0ba38885714c82ffb7eb0d9d95b92a26190b13183a231bd1894
curl: (52) Empty reply from server
~~~~^^^^^^^^^^^^^^^^^
OSError: [Errno 30] Read-only file system: '/data/visits.txt'
----------------------------------------
hello
The app could not write its count, Python raised an error, and the request got no reply. readonly is right for configuration a container should only read, and wrong for data it must write. The logs told you which.
When it goes wrong
The count starts at 1 again. The container has no mount at /data, or a different one. docker inspect --format '{{range .Mounts}}{{.Type}} {{.Name}} {{.Source}} -> {{.Destination}}{{end}}' hello prints nothing when there is no mount. Check the volume name for typos: a misspelled name silently creates a new, empty volume.
bind source path does not exist. --mount type=bind needs the host path to exist. Create it, or fix the path. Use "$PWD"/data for a folder in the current directory; a relative path such as data on its own is read as a volume name by -v.
Permission denied on a file in a bind mount. The container wrote it as root. Run the container with --user "$(id -u):$(id -g)", or fix the owner with sudo chown.
Read-only file system. The mount has readonly, or ro in the short form. Remove it if the container must write there.
volume is in use. docker volume rm refuses while a container, running or stopped, still uses the volume. docker ps -a --filter volume=hello-data finds it.
The image’s files vanished from a folder. A mount hides whatever the image had at that path. Mount somewhere else, or, for an empty named volume, let Docker copy the image’s files in first.
Cheat sheet
| Task | Command | Note |
|---|---|---|
| Create a volume | docker volume create hello-data |
or let docker run create it |
| Mount a volume | --mount type=volume,src=hello-data,dst=/data |
short form -v hello-data:/data |
| Bind-mount a folder | --mount type=bind,src="$PWD"/data,dst=/data |
short form -v "$PWD"/data:/data |
| Read-only mount | ...,dst=/data,readonly |
:ro in the short form |
| List volumes | docker volume ls |
driver and name |
| Volume details | docker volume inspect hello-data |
Mountpoint is Docker’s, do not edit it |
| Which mounts a container has | docker inspect --format '{{range .Mounts}}{{.Type}} {{.Source}} -> {{.Destination}}{{end}}' hello |
volume or bind |
| Run as your user | --user "$(id -u):$(id -g)" |
files in bind mounts stay yours |
| Remove a volume | docker volume rm hello-data |
permanent; fails while in use |
Recap
- Files written to a container’s writable layer are deleted by
docker rm. - A named volume is storage Docker manages. It survives
docker rmuntil you rundocker volume rm. - A bind mount shares a host folder with the container. Both sides see the same files, with the same numeric owners.
--mountis explicit and refuses a missing host path;-vis shorter and creates one.docker inspecton a container shows each mount’s type, source and destination.
Quoted passages in this lesson come from the Docker documentation. Docker documentation is licensed under Apache License 2.0. Docker Official Image READMEs and the hello-world image are licensed under the MIT License, © Docker, Inc., where this lesson uses them. Docker® is a registered trademark of Docker, Inc. Python® is a registered trademark of the Python Software Foundation. aitamer.news is not affiliated with or endorsed by Docker, Inc. or the Python Software Foundation.

The Campfire
CommentsThanks. Your comment is waiting for the desk. Once it is approved it appears here within about a minute.
Add a comment