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

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

问题描述

给定以下 Dockerfile

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

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

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

如果我在容器中执行 su john,我将无法访问测试目录中的任何文件或子目录.这个问题似乎与 aufs 文件系统中的所有权有关,其中复制的目录仍归 root 所有,权限设置为 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.

是否有解决此问题的解决方法来正确设置权限?一种可能是在复制之前将原始目录的权限设置为容器用户的 uid.但这似乎更像是一种 hack.

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

setpermissions 脚本负责设置用户权限:

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

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

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

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

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