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: Docker's mount syntax refuses a missing host folder where -v quietly creates it

Docker
~/tips/tip-docker-mount-missing-source-error

docker run --rm --mount type=bind,src=/srv/apps/data,dst=/data alpine ls /data

Use the mount syntax to catch a typo in a bind mount path before it costs you data.

With -v, a host path that does not exist is created as an empty folder owned by root, and the container starts happily with no data. The --mount syntax refuses instead.

docker run --rm --mount type=bind,src=/srv/apps/data,dst=/data alpine ls /data

If /srv/apps/data is missing, Docker stops with invalid mount config for type "bind": bind source path does not exist. The same mistake with -v /srv/apps/data:/data would create the folder and run the app against it. A database that finds an empty folder will initialise a brand new one, and you only notice when your data seems to be gone.

That is why I prefer --mount for anything where a typo matters. It is longer to type, but each part has a name, so it is also easier to read back later:

--mount type=bind,src=/srv/apps/conf,dst=/etc/app,readonly

Compose’s short syntax behaves like -v and creates missing folders. In the long syntax you can set bind: create_host_path: false to get the strict behaviour there too. On Unraid and other NAS systems, a share that is not mounted yet at boot is the classic trigger; the strict form turns that into a clear error instead of a container writing to the wrong disk.

Further reading: Bind mounts: differences between -v and –mount

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