简体   繁体   English

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

[英]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.

相关问题 使用 docker-compose 和绑定挂载在 Linux 上创建一个 Docker-Postgres-Container,这样该目录包含 pgres-data 并且不属于 root - Create a Docker-Postgres-Container on Linux with docker-compose and a bind-mount, such that the dir contains the pgres-data and is not owned by root Docker Compose - 删除悬空服务 - Docker Compose - Remove dangling service 如何在Docker中挂载Hugepages - How to Mount Hugepages inside Docker 无法绑定在 Linux SQL 服务器中安装 Windows 文件夹 Z05B6053C41A2130AFDZimageBDAE1 - Unable to bind mount a Windows folder in a Linux SQL Server docker image 如何绑定安装保险丝安装点? - How do I bind mount a fuse mountpoint? "如何将主机目录挂载到正在运行的 docker 容器中" - How to mount a host directory into a running docker container 如何为我的 docker 安装本地卷? - how to mount a local volume for my docker? 是否可以将绑定挂载目标的数据复制到本地源文件夹? - Is it possible to copy data of bind mount destination to a local source folder? Docker 使用绑定挂载的主机目录和容器之间的文件权限不匹配 - Docker file permissions mismatch between host dir and container using bind-mount 使用读写权限将数据卷挂载到 docker - Mount data volume to docker with read&write permission
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM