gpt4 book ai didi

docker - 使用无根 docker 创建的绑定(bind)挂载在主机上有一个奇怪的 uid。如何删除这些文件夹?

转载 作者:行者123 更新时间:2023-12-03 14:36:57 31 4
gpt4 key购买 nike

我有以下 docker-compose.yml创建位于 $HOME/test 中的绑定(bind)挂载的文件在主机系统上:

version: '3.8'

services:
pg:
image: postgres:13
volumes:
- $HOME/test:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=pass
- PGUSER=postgres
我调出容器并检查绑定(bind)挂载目录的权限:
$ docker-compose up -d
$ ls -l ~
drwx------ 19 4688518 usertest 4096 Mar 11 17:06 test
文件夹 ~/test使用不同的 uid 创建,以防止在容器外意外操作此文件夹。但如果我真的想操纵它怎么办?例如,如果我尝试删除文件夹,则会按预期收到权限被拒绝错误:
$ rm ~/test -rf
rm: cannot remove '/home/usertest/test': Permission denied
我怀疑我需要使用 newuidmap 更改 uid以某种方式命令,但我不知道该怎么做。

最佳答案

How can I delete these folders?


But what if I really do want to manipulate it?


使用 Docker,您可以:
  • 使用相同的 UID(如 rmsh )以特定用户身份在容器中运行命令,例如:
    # Run shell session using your user with docker-compose
    # You can then easily manipulate data
    docker-compose exec -u 4688518 pg sh

    # Run command directly with docker
    # Docker container name may vary depending on your situation
    # Use docker ps to see real container name
    docker exec -it -u 4688518 stack_pg_1 rm -rf /var/lib/postgresql/data
  • 与上一个类似,您可以运行一个新容器:
    # Will run sh by default
    docker run -it -u 4688518 -v $HOME/test:/tmp/test busybox

    # You can directly delete data with
    docker run -it -u 4688518 -v $HOME/test:/tmp/test busybox rm -rf /tmp/test/*
    如果您的 pg容器被停止或删除。 Docker 镜像本身不需要与 Docker Compose 运行的镜像相同,您只需要指定正确的用户 UID。
    注意:您可能无法使用 rm -rf /tmp/test 删除文件夹作为用户 4688518可能没有对 /tmp 的写入权限文件夹这样做,因此使用 /tmp/test/*
  • 使用上述任何一种,但使用 root 用户,例如 -u 0-u root

  • 不使用 Docker,可以有效运行 sudo other answer 建议的命令,或者甚至暂时更改所述文件夹的权限,然后将其更改回来。然而,根据经验,在操作 Docker 相关数据时,用户 Docker 本身更容易且不易出错。

    关于docker - 使用无根 docker 创建的绑定(bind)挂载在主机上有一个奇怪的 uid。如何删除这些文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66592852/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com