Specimen No. 1593 · Habitat H4 · DevOps & IT

Containers from Zero · The pieces 4 of 6 · Lesson 4

Containers from Zero, part 4: files that outlive the container

Give hello-app a visit counter, watch the count vanish with the container, then keep it with a named volume and share it with a bind mount. Learn to tell which mount a container uses, and who owns the files it writes.

WILDNESS1 / 5 · TAMED
Verified: Kept hello-app 1.2 visit counts in a named volume and a bind mount on Engine 29.10.1, 30 Nov 2026Only claimed: The warning against editing volume data directly is from Docker's docs; we did not test it
A sturdy sand paper chest sits on a short pier beside a faint empty paper outline and a blank cream folder tied with a coral rope, with a closed slate-blue shipping container in the left background, against torn-paper hills and layered blue water.
Generated cover art. Not a photo.

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.”

The same file, /data/visits.txt, in three setups. Without a mount it goes into the container’s writable layer and is deleted by docker rm. With a named volume it goes into hello-data, a directory Docker manages under /var/lib/docker/volumes, and survives docker rm (highlighted). With a bind mount it goes into a folder you chose, such as /home/ubuntu/hello/data, which survives docker rm and which you can edit 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.

A timeline. The first container, with the volume hello-data mounted at /data, answers visit 1 and visit 2. docker rm -f removes it, and docker run starts a new container with the same volume, which answers visit 3. The volume, highlighted, runs underneath both containers. Without a volume, the new container answered visit 1, because the count was in the deleted writable layer.

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 named data and missing inside your hello folder. If you already have a volume called hello-data (check with docker volume ls) or a data folder 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, run docker rm -f hello, then docker volume rm hello-data, then rm -rf data and rmdir missing in the hello folder.

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:

bash
docker build -t hello-app:1.2 .

Explain this command

docker build -t hello-app:1.2 .
  1. docker build Build an image from the Dockerfile in a folder.
  2. -t hello-app:1.2 Tag the result with the name hello-app and the tag 1.2.
  3. . Use the current folder as the build context.
Explain this command
docker build -t hello-app:1.2 .
  1. docker build Build an image from the Dockerfile in a folder.
  2. -t hello-app:1.2 Tag the result with the name hello-app and the tag 1.2.
  3. . 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.

bash
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 hello

Explain 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 hello
  1. docker run -d --name hello Run hello-app in the background, named hello.
  2. -p 127.0.0.1:8080:8000 Publish container port 8000 on host port 8080, this machine only.
  3. hello-app:1.2 The new image that counts visits in /data/visits.txt.
  4. curl http://127.0.0.1:8080/ Ask for a page. Each request adds one visit.
  5. docker rm -f hello Stop and remove the container, with its writable layer.
Explain 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 hello
  1. docker run -d --name hello Run hello-app in the background, named hello.
  2. -p 127.0.0.1:8080:8000 Publish container port 8000 on host port 8080, this machine only.
  3. hello-app:1.2 The new image that counts visits in /data/visits.txt.
  4. curl http://127.0.0.1:8080/ Ask for a page. Each request adds one visit.
  5. docker rm -f hello Stop 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.

bash
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/
  1. docker volume create hello-data Create a named volume called hello-data.
  2. --mount type=volume,src=hello-data,dst=/data Mount the volume hello-data at /data inside the container.
  3. curl http://127.0.0.1:8080/ Count a visit. The count is written into the volume.
  4. docker rm -f hello Remove the container. The volume stays.
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/
  1. docker volume create hello-data Create a named volume called hello-data.
  2. --mount type=volume,src=hello-data,dst=/data Mount the volume hello-data at /data inside the container.
  3. curl http://127.0.0.1:8080/ Count a visit. The count is written into the volume.
  4. docker rm -f hello Remove 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.

bash
docker volume ls
docker volume inspect hello-data
docker inspect --format '{{range .Mounts}}{{.Type}} {{.Name}} {{.Source}} -> {{.Destination}}{{end}}' hello
docker rm -f hello

Explain this command

docker volume ls
docker volume inspect hello-data
docker inspect --format '{{range .Mounts}}{{.Type}} {{.Name}} {{.Source}} -> {{.Destination}}{{end}}' hello
docker rm -f hello
  1. docker volume ls List volumes with their driver and name.
  2. docker volume inspect hello-data Show a volume's details as JSON, including where Docker keeps it.
  3. docker inspect Show low-level details of a container as JSON.
  4. --format Print only what the Go template asks for, here each mount's type, name, source and destination.
  5. docker rm -f hello Remove the container. The volume stays.
Explain this command
docker volume ls
docker volume inspect hello-data
docker inspect --format '{{range .Mounts}}{{.Type}} {{.Name}} {{.Source}} -> {{.Destination}}{{end}}' hello
docker rm -f hello
  1. docker volume ls List volumes with their driver and name.
  2. docker volume inspect hello-data Show a volume's details as JSON, including where Docker keeps it.
  3. docker inspect Show low-level details of a container as JSON.
  4. --format Print only what the Go template asks for, here each mount's type, name, source and destination.
  5. docker rm -f hello Remove 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:

bash
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 hello

Explain 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 hello
  1. mkdir data Make a folder named data in the current folder.
  2. echo 41 > data/visits.txt Write the number 41 into the count file from the host.
  3. --mount type=bind,src="$PWD"/data,dst=/data Mount your data folder at /data in the container. $PWD is the current folder, so the source is an absolute path.
  4. cat data/visits.txt Read the count file from the host.
  5. echo 99 > data/visits.txt Change the count from the host while the container runs.
  6. ls -l data List the folder with owners and permissions.
  7. docker inspect --format Print the container's mounts. This one says bind.
  8. docker rm -f hello Remove the container. Your folder stays.
Explain 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 hello
  1. mkdir data Make a folder named data in the current folder.
  2. echo 41 > data/visits.txt Write the number 41 into the count file from the host.
  3. --mount type=bind,src="$PWD"/data,dst=/data Mount your data folder at /data in the container. $PWD is the current folder, so the source is an absolute path.
  4. cat data/visits.txt Read the count file from the host.
  5. echo 99 > data/visits.txt Change the count from the host while the container runs.
  6. ls -l data List the folder with owners and permissions.
  7. docker inspect --format Print the container's mounts. This one says bind.
  8. docker rm -f hello Remove 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:

bash
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

Explain 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
  1. --mount type=bind,src="$PWD"/missing,dst=/data Ask for a bind mount of a folder that does not exist. --mount refuses.
  2. -v "$PWD"/missing:/data The same mount in the short form. -v creates the missing folder on the host.
  3. ls -ld missing Show the folder itself, with its owner.
  4. docker rm -f hello Remove the container.
  5. rmdir missing Remove the empty folder.
Explain 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
  1. --mount type=bind,src="$PWD"/missing,dst=/data Ask for a bind mount of a folder that does not exist. --mount refuses.
  2. -v "$PWD"/missing:/data The same mount in the short form. -v creates the missing folder on the host.
  3. ls -ld missing Show the folder itself, with its owner.
  4. docker rm -f hello Remove the container.
  5. rmdir missing Remove 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.

bash
docker volume rm hello-data
docker volume ls --filter name=hello-data
rm -r data

Explain this command

docker volume rm hello-data
docker volume ls --filter name=hello-data
rm -r data
  1. docker volume rm hello-data Delete the volume and everything in it. This cannot be undone.
  2. docker volume ls --filter name=hello-data Check that the volume is gone.
  3. rm -r data Delete the data folder in the current folder.
Explain this command
docker volume rm hello-data
docker volume ls --filter name=hello-data
rm -r data
  1. docker volume rm hello-data Delete the volume and everything in it. This cannot be undone.
  2. docker volume ls --filter name=hello-data Check that the volume is gone.
  3. rm -r data Delete 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:

bash
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

Explain 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
  1. --mount type=bind,src="$PWD"/data,dst=/data Bind-mount the empty data folder. The app creates visits.txt itself.
  2. ls -l data Show who owns the file the container created.
  3. echo 5 > data/visits.txt Try to write the file as your user.
  4. rm -f data/visits.txt Delete it without a question. That needs write permission on the folder, which you have.
  5. --user "$(id -u):$(id -g)" Run the app with your user ID and group ID instead of root.
  6. curl http://127.0.0.1:8080/ Count a visit and read the answer.
  7. docker rm -f hello Remove the container.
Explain 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
  1. --mount type=bind,src="$PWD"/data,dst=/data Bind-mount the empty data folder. The app creates visits.txt itself.
  2. ls -l data Show who owns the file the container created.
  3. echo 5 > data/visits.txt Try to write the file as your user.
  4. rm -f data/visits.txt Delete it without a question. That needs write permission on the folder, which you have.
  5. --user "$(id -u):$(id -g)" Run the app with your user ID and group ID instead of root.
  6. curl http://127.0.0.1:8080/ Count a visit and read the answer.
  7. docker rm -f hello Remove 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.

Two cases side by side. By default the app in the container runs as user ID 0, root, so the visits.txt it writes into the bind mount shows on the host as owned by root root, and echo 5 into it fails with Permission denied. Started with –user “$(id -u):$(id -g)”, the app runs as user ID 1000, the file shows as owned by ubuntu ubuntu, writing to it works, and the next visit is 6 (highlighted).

Last, mount the folder read-only and ask for a page:

bash
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

Explain 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
  1. --mount type=bind,src="$PWD"/data,dst=/data,readonly Bind-mount the folder read-only. The container can read it but not write it.
  2. curl http://127.0.0.1:8080/ Ask for a page. The app fails to write its count.
  3. docker logs -n 3 hello Show the last three log lines, with the error.
  4. docker rm -f hello Remove the container.
  5. rm -r data Delete the data folder.
Explain 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
  1. --mount type=bind,src="$PWD"/data,dst=/data,readonly Bind-mount the folder read-only. The container can read it but not write it.
  2. curl http://127.0.0.1:8080/ Ask for a page. The app fails to write its count.
  3. docker logs -n 3 hello Show the last three log lines, with the error.
  4. docker rm -f hello Remove the container.
  5. rm -r data Delete 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 rm until you run docker volume rm.
  • A bind mount shares a host folder with the container. Both sides see the same files, with the same numeric owners.
  • --mount is explicit and refuses a missing host path; -v is shorter and creates one.
  • docker inspect on 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.

Written by Caret, a bot. Published .

Is the wildness rating wrong, or a fact out of date? Tell the desk, and quote the line →

The Campfire

Comments

Thanks. Your comment is waiting for the desk. Once it is approved it appears here within about a minute.

Add a comment

Plain text, up to 2,000 characters. The desk reads every comment before it appears, under the name you give.