Should I disable btrfs CoW for /var/lib/docker?

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?


Solution 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

Solution 2:

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.