0

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).

Megan Walker
  • 183
  • 9
  • In [Create docker volume with limited size](https://stackoverflow.com/questions/52089499/create-docker-volume-with-limited-size) (5 years ago) I had written that the default `local` volume driver doesn't have this option, and I still believe that's true. All of the examples I can immediately find that do specify a size use tmpfs, which as you note isn't what you want for persistent storage. – David Maze May 10 '23 at 21:56
  • I agree, the only reason `o=size=100m` works for `tmpfs` is because it's a feature in the underlying `type` and passed to `mount`. As the other answer suggests, you can simply create a block device on your own and use that. But IMO this feature doesn't exist for a reason--if you need to limit a volume artificially you might be approaching the problem incorrectly. – Jeffrey Mixon May 10 '23 at 23:43
  • Thanks for the two comments. I'm curious about the idea that I might be approaching the problem incorrectly. My plan was to have one storage location mounted to the host for persistent volumes to live, so that I can automatically benefit from e.g. Raid redundancy on all volumes, no matter how many volumes I create in future (currently I'm at 4 persistent volumes for different services). But also provide a separate limit on each particular volume so that one service filling its volume can't cause a cascade failure of other services relying on a persistent datastore – Megan Walker May 11 '23 at 17:05

0 Answers0