Docker在挂载卷中以root用户身份创建文件 [英] Docker creates files as root in mounted volume
问题描述
我正在使用Docker(1.3.1)在容器内构建RPM:
I'm using Docker (1.3.1) to build RPMs inside a container:
docker run -v /home/matt/build:/build build-rpm /build/build-pkg.sh
这很好用(我的用户在docker
组中,所以我不需要sudo)并将一个完整的.rpm
文件拖放到当前目录中.问题在于该文件是由root拥有的.
This works fine (my user is in the docker
group, so I don't need to sudo) and drops a completed .rpm
file in the current directory. The problem is that the file is created as owned by root.
我该如何安排文件以便创建与运行docker的用户相同的用户?
How can I arrange it so that the file is created owned by the same user as I run docker with?
推荐答案
您可以尝试创建一个用户(在自定义映像的Dockerfile中)并将其设置为容器使用的用户
You could try to create (in the Dockerfile of a custom image) a user and set it as the one used by the container
RUN adduser --system --group --shell /bin/sh auser \
&& mkdir /home/auser/bin
USER auser
然后检查docker run -v /home/matt/build:/build build-rpm
是否将auser
.
问题2259 中提到的另一个选项:
Another option mentioned in issue 2259:
如果在绑定安装之前在主机上在主机上
chown
卷,则该卷将起作用.
在这种情况下,您可以执行以下操作:
If you
chown
the volume (on the host side) before bind-mounting it, it will work.
In that case, you could do:
mkdir /tmp/www
chown 101:101 /tmp/www
docker run -v /tmp/www:/var/www ubuntu stat -c "%U %G" /var/www
(假设
101:101
是容器中www-data
用户的UID:GID
.)
(Assuming that
101:101
is theUID:GID
of thewww-data
user in your container.)
这篇关于Docker在挂载卷中以root用户身份创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!