Docker复制和更改所有者 [英] Docker Copy and change owner

查看:290
本文介绍了Docker复制和更改所有者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下Dockerfile

  FROM ubuntu 
RUN groupadd mygroup
RUN useradd -ms / bin / bash -G mygroup john
MKDIR / data
COPY测试/ / data / test数据
RUN chown -R john:mygroup / data
CMD / bin / bash

在复制的测试目录中,我已将文件权限设置为770。



如果在容器中执行 su john ,则无法访问测试目录中的任何文件或子目录。似乎此问题与aufs文件系统中的所有权有关,在该文件系统中,复制的目录仍由root拥有,并且权限设置为770。



是否存在针对此问题的解决方法这个问题正确设置权限?一种可能是在复制之前将原始目录的权限设置为容器用户的uid。

解决方案

我认为我找到了一个可行的解决方案。使用数据卷容器可以解决问题。首先,我创建一个数据卷容器,其中包含我的外部目录的副本:

  FROM busybox 
RUN mkdir / data
卷/ data
COPY / test / data / test
CMD / bin / sh

在我的应用容器中有我的用户,看起来像这样

  FROM ubuntu 
RUN groupadd mygroup
RUN useradd -ms / bin / bash -G mygroup john
COPY setpermissions.sh /root/setpermissions.sh
CMD /root/setpermissions.sh&& / bin / bash

setpermissions脚本可以完成设置用户权限的工作:


< pre class = lang-bash prettyprint-override> #!/ bin / bash

如果[! -e /data/.bootstrapped];然后
chown -R john:mygroup / data
touch /data/.bootstrapped
fi

现在,我只需要在运行应用程序容器时使用-volumes-from< myDataContainerId>


Given the following Dockerfile

FROM ubuntu
RUN groupadd mygroup
RUN useradd -ms /bin/bash -G mygroup john
MKDIR /data
COPY test/ /data/test data
RUN chown -R john:mygroup /data
CMD /bin/bash

In my test directory, which is copied I have set the file permissions to 770.

If I do a su john inside my container, I cannot access any of the files or subdirectories in my test directory. It seems this problem is related to the ownership in the aufs filesystem, where the copied directory still is owned by root and permissions are set to 770.

Is there a workaround for this problem to set the permissions correctly? One could be to set the permissions of the original directory to the uid of the container user before copying it. But this seems more like a hack.

解决方案

I think I found a solution, which works. Using a data volume container will do the trick. First I create the Data Volume Container, which contains the copy of my external directory:

FROM busybox
RUN mkdir /data
VOLUME /data
COPY /test /data/test
CMD /bin/sh

In my application container, where I have my users, which could look something like this

FROM ubuntu
RUN groupadd mygroup
RUN useradd -ms /bin/bash -G mygroup john
COPY setpermissions.sh /root/setpermissions.sh
CMD /root/setpermissions.sh && /bin/bash

The setpermissions script does the job of setting the user permissions:

#!/bin/bash

if [ ! -e /data/.bootstrapped ] ; then
  chown -R john:mygroup /data
  touch /data/.bootstrapped
fi

Now I just have to use the --volumes-from <myDataContainerId> when running the application container.

这篇关于Docker复制和更改所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆