如何控制哪个用户拥有我要复制到容器中或从容器中复制的文件?
docker cp
command表示有关文件所有权的信息:
The
cp
command behaves like the Unixcp -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 withUID:GID
of the root user. Files copied to the local machine are created with theUID:GID
of the user which invoked thedocker 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/