brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: volume-subpath mounts a single folder of a volume into a container

Docker
~/tips/tip-docker-run-mount-volume-subpath

docker run --rm --mount src=logs,dst=/logs alpine mkdir -p /logs/app1 /logs/app2

Mounts only a subdirectory of a named volume, so several containers can share one volume without seeing each other.

One named volume for several apps is tidy, until every app can see the others’ files. A subpath mount limits each container to its own folder.

docker run --rm --mount src=logs,dst=/logs alpine mkdir -p /logs/app1 /logs/app2

docker run -d \
  --name=app1 \
  --mount src=logs,dst=/var/log/app1,volume-subpath=app1 \
  app1:latest

Both commands are from the Docker volumes docs. The first creates two folders in the volume called logs. The second mounts only app1 at /var/log/app1, so that container never sees app2.

Two rules from the docs. You must use --mount, because the docs say you must use it to mount a volume subdirectory. And the subdirectory must exist in the volume before you mount it, otherwise the mount fails.

That second rule is the caveat I would remember. Nothing creates the folder for you, so the first run needs a small step like the mkdir above. The docs page does not name a minimum Docker version, so if the option is rejected on an old install, check the version first.

It is a neat way to give a log collector or a backup job access to one app’s data without mounting the whole volume.

Further reading: Docker volumes: mount a volume subdirectory

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link