Docker Compose,如何去除bind mount数据?

Docker Compose, how to remove bind mount data?

我在 Debian 11 上使用 Docker Compose 创建一个 PostgreSQL 数据库。

当我尝试关闭 Docker 组合并删除所有现有数据时,我似乎无法摆脱绑定装载的数据。

docker-compose.yml

version: '3'

services:
  postgres:
    image: postgres:9.3.25
    healthcheck:
      test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
      timeout: 45s
      interval: 10s
      retries: 10
    restart: always
    environment:
      - POSTGRES_USER=root
      - POSTGRES_PASSWORD=docker
      - APP_DB_USER=app_user
      - APP_DB_PASS=docker
      - APP_DB_NAME=myapp_db
    # Notice this creates bind mounts, not volumes!
    volumes:
      - ./db-entry-point:/docker-entrypoint-initdb.d/
      - ./postgres-db-data:/var/lib/postgresql/data
    ports:
      - 5432:5432

发出以下命令以关闭 docker 个容器。我知道默认情况下它不会删除卷。 --volumes 参数可用于删除卷,但我测试它不会删除绑定土墩数据。

docker compose -f /opt/storage/disk-03/docker/myapp-db/docker-compose.yml down

这些文件位于绑定安装目标目录中:

ls -la /opt/storage/disk-03/docker/myapp-db/postgres-db-data/
total 44
drwx------ 15 systemd-timesync root               329 Mar 29 09:52 .
drwxr-xr-x  4 root             root                78 Mar 29 09:35 ..
-rw-------  1 systemd-timesync systemd-timesync     4 Mar 28 14:52 PG_VERSION
drwx------  7 systemd-timesync systemd-timesync    67 Mar 28 14:52 base
drwx------  2 systemd-timesync systemd-timesync  4096 Mar 29 09:51 global
drwx------  2 systemd-timesync systemd-timesync    18 Mar 28 14:52 pg_clog
-rw-------  1 systemd-timesync systemd-timesync  4486 Mar 28 14:52 pg_hba.conf
-rw-------  1 systemd-timesync systemd-timesync  1636 Mar 28 14:52 pg_ident.conf
drwx------  4 systemd-timesync systemd-timesync    36 Mar 28 14:52 pg_multixact
drwx------  2 systemd-timesync systemd-timesync    18 Mar 29 09:50 pg_notify
drwx------  2 systemd-timesync systemd-timesync     6 Mar 28 14:52 pg_serial
drwx------  2 systemd-timesync systemd-timesync     6 Mar 28 14:52 pg_snapshots
drwx------  2 systemd-timesync systemd-timesync   105 Mar 29 09:52 pg_stat
drwx------  2 systemd-timesync systemd-timesync     6 Mar 29 09:52 pg_stat_tmp
drwx------  2 systemd-timesync systemd-timesync    18 Mar 28 14:52 pg_subtrans
drwx------  2 systemd-timesync systemd-timesync     6 Mar 28 14:52 pg_tblspc
drwx------  2 systemd-timesync systemd-timesync     6 Mar 28 14:52 pg_twophase
drwx------  3 systemd-timesync systemd-timesync    60 Mar 28 14:52 pg_xlog
-rw-------  1 systemd-timesync systemd-timesync 20120 Mar 28 14:52 postgresql.conf
-rw-------  1 systemd-timesync systemd-timesync    37 Mar 29 09:50 postmaster.opts

我不确定如何删除这些文件,因为 rm -rf 没有删除它们。 ls -la /opt/storage/disk-03/docker/myapp-db/postgres-db-data/ 仍然列出文件。

sudo rm -rf /opt/storage/disk-03/docker/myapp-db/postgres-db-data/*

从 Docker 的角度来看,删除这些文件的正确方法是什么?

似乎可以通过先更改文件所有者然后删除来删除文件。我不确定底层 OS 中发生了什么,但我测试了这几次并且它有效。

# This doesn't remove the files when files are owned by "systemd-timesync"
sudo rm -rf /opt/storage/disk-03/docker/myapp-db/postgres-db-data/*

# chown to admin user
sudo chown -R admin. /opt/storage/disk-03/docker/myapp-db/postgres-db-data/
# Now removal works
sudo rm -rf /opt/storage/disk-03/docker/myapp-db/postgres-db-data/*