[英]Docker Compose, how to remove bind mount data?
I'm creating a PostgreSQL database with Docker Compose on Debian 11.我在 Debian 11 上使用 Docker Compose 创建一个 PostgreSQL 数据库。
Docker version 20.10.14, build a224086
Docker Compose version v2.3.3
When I try to shutdown Docker Compose and remove all existing data, I cannot seem to get rid of bind mounted data.当我尝试关闭 Docker Compose 并删除所有现有数据时,我似乎无法摆脱绑定装载的数据。
docker-compose.yml 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
The command below is issued to shutdown docker containers.发出以下命令以关闭 docker 个容器。 I know it doesn't remove volumes by default.
我知道默认情况下它不会删除卷。
--volumes
argument could be used for removing volumes, but I tested that it doesn't remove bind mound data. --volumes
参数可用于删除卷,但我测试它不会删除绑定土墩数据。
docker compose -f /opt/storage/disk-03/docker/myapp-db/docker-compose.yml down
These files are on the bind mount target directory:这些文件位于绑定安装目标目录中:
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
I'm not sure how to delete these files as rm -rf
doesn't remove them.我不确定如何删除这些文件,因为
rm -rf
不会删除它们。 ls -la /opt/storage/disk-03/docker/myapp-db/postgres-db-data/
still lists the files. 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/*
What would be the right way from Docker's perspective to remove these files?从 Docker 的角度来看,删除这些文件的正确方法是什么?
Seems that the files can be removed by changing the owner of the files first and then removing.似乎可以通过先更改文件的所有者然后删除来删除文件。 I'm not sure what's going on in the underlying OS, but I tested this couple times and it works.
我不确定底层操作系统中发生了什么,但我测试了这几次并且它有效。
# 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/*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.