5

I saw that it is not a good idea to use btrfs CoW functionality for big files, such as data directories of a PostgreSQL database.

Since I use docker for databases, I now ask myself, if I should disable CoW for the whole /var/lib/docker directory. But I'm not sure, because docker's layered filesystem makes use of this feature, or not?

Or is it possible, to disable CoW for just some specific volumes?

2 Answers 2

1

I don't think docker directly plugs into BTRFS (or any filesystem) CoW behavior; rather, it can use snapshots and/or reflinks to avoid replicating entire container images.

While disabling CoW will be surely benefical for performance, be aware that:

  • disabling it means no data checksum, so no protection against data loss;
  • snapshotting a volume automatically re-enables CoW;
  • reflink will not work anymore;
  • existing file will remain CoW-enabled (until you delete and recreate them).

While for a database (as PostgreSQL) the above points can be non-issues (ie: data checksum is done at the DB record level and snapshots are provided by the transactional layer), for a VM or container missing these features can be problematic.

Anyway, I really suggest you to read the BTRFS FAQ about nodatacow

3
  • 2
    When using docker on top of a btrfs filesystem, it creates a directory called /var/lib/docker/btrfs containing BTRFS subvolumes. So this looks like docker plugs into filesystem specific features. Commented Oct 8, 2019 at 13:51
  • Thank you @shodanshok! I'm a bit confused about phrase "snapshotting a volume automatically re-enables CoW", I thought that if I make a snapshot of a volume, then it will do CoW only once to avoid changing the file in the snapshot, am I wrong then? Commented Oct 2, 2021 at 12:54
  • 1
    @Slabko you are right, but consider that frequent snapshots will cause many CoW "cycles"
    – shodanshok
    Commented Oct 2, 2021 at 15:16
1

Put the following in /etc/docker/daemon.json:

{
    "storage-driver": "btrfs"
}

and disable copy-on-write on your volumes if they contain databases:

mkdir /var/lib/docker/volumes
chattr +C /var/lib/docker/volumes

Hoping that helps.

1
  • It's important, that the directory does not exists yet. chattr +C on a directory does only affect new files and directories in it. Enabling nodatacow on /var/lib/docker/volumes is only affecting new volumes. You should not disable CoW totally. I recommend to use a prepared directory as bind mount for database files and writeable image files.
    – sausix
    Commented Jan 18, 2021 at 4:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .