A Compose File with Both Kinds
services:
db:
image: mariadb:11.4
env_file: .env
volumes:
- dbdata:/var/lib/mysql
web:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html:ro
volumes:
dbdata:
dbdata is a named volume: Docker creates and manages it. ./html is a bind mount: a folder next to the Compose file, shown inside the container, read-only because of :ro. The rule: a source written as a path, starting with /, ./, or ../, is a bind mount; a plain name is a named volume and must be listed under the top-level volumes key. The database password goes into .env with mode 600, not into the Compose file. The two kinds are compared in general in Docker volumes vs bind mounts.
Long Syntax
The same mounts written out in full. It is longer, but every option has its own line:
volumes:
- type: volume
source: dbdata
target: /var/lib/mysql
- type: bind
source: ./html
target: /usr/share/nginx/html
read_only: true
The Top-Level volumes Key
Compose prefixes named volumes with the project name, which defaults to the folder name: dbdata in a folder called shop becomes shop_dbdata. Two options change that:
volumes:
dbdata:
name: shop_dbdata
uploads:
external: true
name: fixes the real volume name, so renaming the folder does not create a new, empty volume.external: true: the volume already exists and Compose never creates or deletes it. Create it once withdocker volume create uploads; if it is missing, Compose stops with an error.
Where Named Volumes Live
docker volume ls
docker volume inspect shop_dbdata
The Mountpoint field shows the folder, normally under /var/lib/docker/volumes/ followed by the volume name and _data. Read it there if you must, but do not edit files while a container uses them.
Ownership Problems with Bind Mounts
A bind mount keeps the owner numbers of the server's files. If the process in the container runs as user 1000 and the folder belongs to root, writes fail with "Permission denied". Compare both sides, here for a service called app that mounts ./data:
docker compose exec app id
ls -ln ./data
Then give the folder to that user ID, for example sudo chown -R 1000:1000 ./data, or set user: "1000:1000" on the service when the image supports it. Never use chmod 777. Named volumes avoid most of this: when an empty named volume is first mounted, Docker copies the image's files at that path into it, owners included.
down -v Deletes Named Volumes
docker compose down removes containers and networks and keeps volumes. docker compose down -v also deletes the named volumes declared in the file and anonymous volumes attached to the containers, and with them the database. Volumes marked as external are never removed. Read docker compose down before you add the flag.
Back Up a Volume with a Helper Container
docker compose stop db
docker run --rm -v shop_dbdata:/data:ro -v "$PWD":/backup alpine \
tar czf /backup/dbdata-$(date +%F).tar.gz -C /data .
docker compose start db
A short-lived alpine container mounts the volume read-only and writes an archive into the current folder. Stopping the database first gives a consistent copy; for a live database, its own dump tool is the better choice. Move the archive off the server afterward.
tmpfs Mounts
services:
web:
tmpfs:
- /tmp
A tmpfs mount lives in memory and is lost when the container stops. It suits scratch files and caches that must not reach the disk.
SELinux: the :z and :Z Labels
On AlmaLinux and Rocky Linux with SELinux enforcing, a bind mount can be refused even with correct owners. Add :Z to relabel the folder for one container (./data:/data:Z) or :z for a folder several containers share. Use them only on folders that belong to the application, never on /home, /etc, or /var, because relabeling a system folder breaks the host. Background is in what is SELinux.