Exercise: volumes, bind mounts and centralized log collection

  1. Step 1 — Find out where Docker is using disk space
  2. Step 2 — Create a volume and write data from a self-deleting container
  3. Step 3 — Read the data back from another container
  4. Step 4 — Bind mount for a static website
  5. Step 5 — Verify read-only mode and the data after deleting the container
  6. Step 6 — Clean up volumes in a controlled way
  7. Step 7 — Limit logs before they fill the disk
  8. Step 8 — Build the centralized logging system with Docker itself
  9. Step 9 — Install the log collection agent and grant permissions
  10. Step 10 — Generate logs and verify
  11. Illustration 1 — docker system df
  12. Illustration 2 — A volume outlives its container
  13. Illustration 3 — docker volume inspect
  14. Illustration 4 — A read-only bind mount
  15. Illustration 5 — Delete the container, host data stays intact
  16. Illustration 6 — Log limits for a container
  17. Illustration 7 — A minimal Vector configuration
  18. Illustration 8 — vector validate
  19. References
  20. Related articles

Estimated time: 50 minutes.

On a Linux host with Docker installed (for example Ubuntu 24.04), work through both ways of persisting data — volumes and bind mounts — and then build a centralized log pipeline for every container on the machine.

Acceptance criteria: (1) a second container can read back a file written to the volume by the first container (which has since been deleted);

(2) editing a file on the host changes the web page immediately, the file on the host survives after the container is removed, and the :ro mode blocks writes from inside the container;

(3) docker system df correctly reflects what is taking up space and a newly started container has a log size limit;

(4) the logs of a freshly created container with generated traffic show up in the centralized log UI and can be filtered by container name.

Step 1 — Find out where Docker is using disk space

Before creating more data, record a baseline to compare against later. Pay attention to the partition that holds the Docker Root Dir, not just the total disk size.

docker info --format "{{.DockerRootDir}}"
du -sh /var/lib/docker
docker system df
df -h /var/lib/docker

Note: When the partition holding the Docker Root Dir fills up, both the daemon and the running containers hit write errors (docker system df).

Step 2 — Create a volume and write data from a self-deleting container

Use --rm so the container disappears as soon as it finishes, which shows that the data does not depend on the container that created it.

docker volume create lab-vol
docker volume ls
docker volume inspect lab-vol
docker run --rm -v lab-vol:/data alpine sh -c "date > /data/created; cat /data/created"
docker ps -a

Note: The Mountpoint field in inspect points to a real directory on the host (Docker: volumes).

Step 3 — Read the data back from another container

This is the most important acceptance check for the volume part. The first container is gone, but the content is intact.

docker run --rm -v lab-vol:/data alpine cat /data/created
ls -l /var/lib/docker/volumes/lab-vol/_data

Note: The printed content must match the exact timestamp written by the container in step 2.

Step 4 — Bind mount for a static website

Prepare the content on the host, then mount it read-only into the Nginx web root. Verify by editing the file on the host and reloading the page.

mkdir -p /opt/html
echo "<h1>Trang do host cap</h1>" > /opt/html/index.html
docker run -d --name lab-web2 -p 8080:80 -v /opt/html:/usr/share/nginx/html:ro nginx
curl -s http://localhost:8080
echo "<h1>Noi dung da sua</h1>" > /opt/html/index.html
curl -s http://localhost:8080

Note: The new content shows up immediately without restarting the container — the container reads the host directory directly (Docker: bind mounts).

Step 5 — Verify read-only mode and the data after deleting the container

Try writing from inside the container (it must be rejected), then delete the container and check the directory on the host again.

docker exec lab-web2 sh -c "echo test > /usr/share/nginx/html/x.html"
docker rm -f lab-web2
ls -l /opt/html
cat /opt/html/index.html

Note: The write is rejected because of the :ro suffix; after the container is deleted, the file on the host is untouched because it belongs to the host.

Step 6 — Clean up volumes in a controlled way

List the dangling volumes first, cross-check them, and only then clean up. prune deletes permanently, with no undo.

docker volume ls
docker volume ls -f dangling=true
docker volume rm lab-vol
docker system df

Note: A volume whose container was just removed is still treated by prune as unused — CI environments that generate volumes continuously need a controlled cleanup schedule (Docker: pruning).

Step 7 — Limit logs before they fill the disk

Find the container that uses the most log space, then run a new container with a limit on log size and number of log files.

du -sh /var/lib/docker/containers/* | sort -h | tail -5
docker run -d --name web-limited -p 8080:80 --log-opt max-size=10m --log-opt max-file=3 nginx
docker inspect --format "{{.HostConfig.LogConfig}}" web-limited

Note: To apply this to every new container on the host, set log-driver and log-opts in /etc/docker/daemon.json and restart Docker (Docker: configure logging).

Step 8 — Build the centralized logging system with Docker itself

Run VictoriaLogs with a volume for its data, so the collected logs are not lost when the container is replaced.

docker volume create victoria-logs-data
docker run -d --name victoria-logs --restart unless-stopped -p 9428:9428 -v victoria-logs-data:/victoria-logs-data victoriametrics/victoria-logs:latest
docker ps
curl -s http://localhost:9428/health

Note: The query UI is served on port 9428 (VictoriaLogs).

Step 9 — Install the log collection agent and grant permissions

Use Vector with the docker_logs source because it reads the logs of every container without needing any paths to be declared. validate must come back clean before you start the service.

Install Vector on Ubuntu 24.04 the official way via the APT repository: the setup.vector.dev script adds the repository, and then you install the vector package (the package creates the vector user and the systemd service for you).

bash -c "$(curl -L https://setup.vector.dev)"
sudo apt-get install -y vector
vector --version

Write a minimal configuration: one source that reads Docker, and one sink that pushes to the VictoriaLogs instance from step 8 (same host, so use 127.0.0.1).

sudo tee /etc/vector/vector.yaml >/dev/null <<'EOF'
sources:
  docker:
    type: docker_logs

sinks:
  victoria:
    type: elasticsearch
    inputs: [docker]
    endpoints: ["http://127.0.0.1:9428/insert/elasticsearch/"]
    mode: bulk
    api_version: v8
    healthcheck:
      enabled: false
    query:
      _msg_field: message
      _time_field: timestamp
      _stream_fields: container_name
EOF

Grant the vector user permission to read Docker, validate the configuration, then start the service:

usermod -aG docker vector
vector validate --config /etc/vector/vector.yaml
systemctl enable vector
systemctl restart vector
systemctl status vector --no-pager

Note: If the configuration is invalid, the service will not start; if the user running the agent is not in the docker group,

the agent starts but collects nothing (Vector:APT,

Vector:docker_logs).

Step 10 — Generate logs and verify

Run one more container, generate some traffic, then find its logs in the centralized log UI using a filter on the container name.

docker run -d --name lab-web3 -p 8081:80 nginx
for i in $(seq 1 50); do curl -s localhost:8081/req-$i >/dev/null; done
docker logs --tail 5 lab-web3

Note: This container is created AFTER the agent is already running — the fact that its logs still arrive is proof that the agent picks up new containers on its own.

Illustration 1 — docker system df

Read what is taking up space and what can be reclaimed.

docker system df

Output:

TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          12        4         3.8GB     2.6GB (68%)
Containers      9         3         1.2GB     900MB (75%)
Local Volumes   7         2         5.4GB     4.1GB (76%)
Build Cache     31        0         2.1GB     2.1GB

Note: The RECLAIMABLE column is what can be cleaned up; Local Volumes in particular need careful cross-checking because they often hold real data.

Illustration 2 — A volume outlives its container

The first container deleted itself, but the second container can still read the data.

docker run --rm -v lab-vol:/data alpine sh -c "date > /data/created; cat /data/created"
docker ps -a
docker run --rm -v lab-vol:/data alpine cat /data/created

Output:

Sat Aug  2 03:14:07 UTC 2026
CONTAINER ID   IMAGE   STATUS    NAMES
Sat Aug  2 03:14:07 UTC 2026

Note: The container list is empty between the two reads — the data belongs to the volume, not to the container.

Illustration 3 — docker volume inspect

This command shows where the volume actually lives on the host.

docker volume inspect lab-vol

Output:

[
    {
        "Driver": "local",
        "Mountpoint": "/var/lib/docker/volumes/lab-vol/_data",
        "Name": "lab-vol",
        "Scope": "local"
    }
]

Note: The Mountpoint is a real directory — running ls there shows exactly the file the container wrote.

Illustration 4 — A read-only bind mount

The container cannot write, while the host can still edit as usual.

docker run -d --name lab-web2 -p 8080:80 -v /opt/html:/usr/share/nginx/html:ro nginx
docker exec lab-web2 sh -c "echo test > /usr/share/nginx/html/x.html"

Output:

sh: can't create /usr/share/nginx/html/x.html: Read-only file system

Note: The :ro suffix limits the damage if a process inside the container is compromised.

Illustration 5 — Delete the container, host data stays intact

docker rm -f lab-web2
ls -l /opt/html

Output:

lab-web2
total 4
-rw-r--r-- 1 root root 34 Aug  2 03:20 index.html

Note: A bind mount maps a host directory; deleting the container only removes the mapping.

Illustration 6 — Log limits for a container

Confirm that the log limit configuration has been applied.

docker inspect --format "{{.HostConfig.LogConfig}}" web-limited

Output:

{json-file map[max-file:3 max-size:10m]}

Note: Without a limit, the log file grows without bound until the container is deleted.

Illustration 7 — A minimal Vector configuration

The configuration consists of one source that reads Docker and one sink that pushes to VictoriaLogs.

cat /etc/vector/vector.yaml

Output:

sources:
  docker:
    type: docker_logs

sinks:
  victoria:
    type: elasticsearch
    inputs: [docker]
    endpoints: ["http://127.0.0.1:9428/insert/elasticsearch/"]
    mode: bulk
    api_version: v8
    healthcheck:
      enabled: false
    query:
      _msg_field: message
      _time_field: timestamp
      _stream_fields: container_name

Note: Change the endpoint to the IP of the machine running VictoriaLogs when the two services are on different hosts.

Illustration 8 — vector validate

The configuration must validate cleanly before Vector can start.

vector validate --config /etc/vector/vector.yaml

Output:

√ Loaded ["/etc/vector/vector.yaml"]
√ Component configuration
√ Health check "victoria"
-------------------------------------
                Validated

Note: If this step reports errors, fix them until it is clean, and only then run systemctl restart vector.

References

Related articles

Exercise: volumes, bind mounts and centralized log collection