Take the following example Docker Compose YAML, where I'm attempting to store a persistent database in a Named Volume that's managed through Docker Compose, which is based heavily upon the Postgresql images example of a Docker Compose setup for Postgresql
version: '3'
services:
postgresql:
restart: unless-stopped
container_name: postgresql
image: postgres:14-alpine
shm_size: 256mb
healthcheck:
test: ['CMD', 'pg_isready', '-U', 'postgres']
volumes:
- postgres-data:/var/lib/postgresql/data
env_file:
- env-files/postgresql.env
volumes:
postgres-data:
As it stands, I believe this will create a volume using the local
driver [cite] that is unrestricted in size.
I have found examples using the tmpfs
storage type, to set a maximum size for a docker compose volume, like so
volumes:
tmpfs:
# For details, see:
# https://docs.docker.com/engine/reference/commandline/volume_create/#driver-specific-options
driver: local
driver_opts:
o: "size=$TMPFS_SIZE"
device: tmpfs
type: tmpfs
But I do not believe a tmpfs will persist data across restarts. Not good for a database. I also believe the o: size
option is specific to tmpfs as whilst it appears in the tmpfs man page, it does not appear in the general mount manpage which the Docker documentation says the o
option is passed to
I did find a Docker CLI option documented under docker volume create
of --limit-bytes
but I'm not able to find an equivilent for Docker Compose.
What is the right way to set a maximum size for a persistent volume that is created and managed via Docker Compose? (Although my example is v3, I could switch to a v2 Docker Compose file if that makes a difference, I believe that v3 is not a full replacement for v2 at this time).