Issue
I'm creating a PostgreSQL database with Docker Compose on Debian 11.
When I try to shutdown Docker Compose and remove all existing data, I cannot seem to get rid of bind mounted data.
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. 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.
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. ls -la /opt/storage/disk-03/docker/myapp-db/postgres-db-data/
still lists the files.
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?
Solution
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/*
Answered By - Eastman Answer Checked By - Pedro (WPSolving Volunteer)