docker - docker cp之后的文件所有权

标签 docker file-permissions

如何控制哪个用户拥有我要复制到容器中或从容器中复制的文件?

docker cp command表示有关文件所有权的信息:

The cp command behaves like the Unix cp -a command in that directories are copied recursively with permissions preserved if possible. Ownership is set to the user and primary group at the destination. For example, files copied to a container are created with UID:GID of the root user. Files copied to the local machine are created with the UID:GID of the user which invoked the docker cp command. However, if you specify the -a option, docker cp sets the ownership to the user and primary group at the source.



它说复制到容器的文件是作为root用户创建的,但这不是我看到的。我创建了两个文件,分别由用户ID 1005和1006拥有。这些所有者被转换为容器的用户 namespace 。当我将文件复制到容器中时,-a选项似乎没有什么区别。
$ sudo chown 1005:1005 test.txt
$ ls -l test.txt
-rw-r--r-- 1 1005 1005 29 Oct  6 12:43 test.txt
$ docker volume create sandbox1
sandbox1
$ docker run --name run1 -vsandbox1:/data alpine echo OK
OK
$ docker cp test.txt run1:/data/test1005.txt
$ docker cp -a test.txt run1:/data/test1005a.txt
$ sudo chown 1006:1006 test.txt
$ docker cp test.txt run1:/data/test1006.txt
$ docker cp -a test.txt run1:/data/test1006a.txt
$ docker run --rm -vsandbox1:/data alpine ls -l /data
total 16
-rw-r--r--    1 1005     1005            29 Oct  6 19:43 test1005.txt
-rw-r--r--    1 1005     1005            29 Oct  6 19:43 test1005a.txt
-rw-r--r--    1 1006     1006            29 Oct  6 19:43 test1006.txt
-rw-r--r--    1 1006     1006            29 Oct  6 19:43 test1006a.txt

当我从容器中复制文件时,它们始终归我所有。同样,-a选项似乎什么也没做。
$ docker run --rm -vsandbox1:/data alpine cp /data/test1006.txt /data/test1007.txt
$ docker run --rm -vsandbox1:/data alpine chown 1007:1007 /data/test1007.txt
$ docker cp run1:/data/test1006.txt .
$ docker cp run1:/data/test1007.txt .
$ docker cp -a run1:/data/test1006.txt test1006a.txt
$ docker cp -a run1:/data/test1007.txt test1007a.txt
$ ls -l test*.txt
-rw-r--r-- 1 don  don  29 Oct  6 12:43 test1006a.txt
-rw-r--r-- 1 don  don  29 Oct  6 12:43 test1006.txt
-rw-r--r-- 1 don  don  29 Oct  6 12:47 test1007a.txt
-rw-r--r-- 1 don  don  29 Oct  6 12:47 test1007.txt
-rw-r--r-- 1 1006 1006 29 Oct  6 12:43 test.txt
$ 

最佳答案

除了@Don Kirkby的答案外,让我在bash/shell脚本中提供一个类似的示例,以供您在将与原始文件的所有权和权限不同的情况下复制到容器中的情况。

让我们从一个小图像创建一个新容器,该容器将继续运行:

docker run -d --name nginx nginx:alpine

现在,我们将创建一个新文件,该文件由当前用户拥有并具有默认权限:
touch foo.bar
ls -ahl foo.bar
>> -rw-rw-r-- 1 my-user my-group 0 Sep 21 16:45 foo.bar

将此文件复制到容器中,将所有权和组设置为我的用户的UID并保留权限:
docker cp foo.bar nginx:/foo.bar
docker exec nginx sh -c 'ls -ahl /foo.bar'
>> -rw-rw-r--    1 4098     4098           0 Sep 21 14:45 /foo.bar

但是,通过使用一些tar解决方法,我可以更改在容器内部应用的所有权和权限。
tar -cf - foo.bar --mode u=+r,g=-rwx,o=-rwx --owner root --group root | docker cp - nginx:/
docker exec nginx sh -c 'ls -ahl /foo.bar'
>> -r--------    1 root     root           0 Sep 21 14:45 /foo.bar
tar选项说明:
  • c创建一个新的存档,而不是解压缩一个存档。
  • f -将写入stdout而不是文件。
  • foo.bar是要打包的输入文件。
  • --mode指定目标的权限。与chown相似,它们可以符号表示或八进制数形式给出。
  • --owner设置文件的新所有者。
  • --group设置文件的新组。
  • docker cp -stdin读取要复制到容器中的文件。

    当文件需要在开始之前复制到创建的容器中时,这种方法很有用,例如docker exec不是选项(只能在正在运行的容器上运行)。

    关于docker - docker cp之后的文件所有权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46613326/

    相关文章:

    Docker nginx proxy, open ldap and phpldapadmin - 无法通过nginx访问ldap admin

    amazon-web-services - ECS on EC2 同一任务中两个容器之间的双向通信

    java - 在 Android 上将大文件从 SD 卡复制到内部存储器

    go - 在 Go 中创建/更新 google drive 权限/[使用 api 创建电子表格但无权访问它]

    python - Windows 客户端无法与在 linux docker 容器中运行的服务器通信

    docker - 滚动更新后如何删除关闭的 Docker 服务任务?

    php - 检查文件权限

    linux - 权限问题,无法以 root 身份运行脚本

    docker - Docker swarm节点以Leader身份运行和Manager身份之间的区别

    linux - 更改公共(public) HTML 中的所有者和组