加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何创建与主机用户具有相同文件权限的docker镜像/容器

发布时间:2020-12-16 03:28:15 所属栏目:安全 来源:网络整理
导读:在组“usergroup1”中以名称“username1”的用户启动docker容器时. 并且该容器在本地文件系统上具有卷的文件/文件夹: 例如. $username1docker run -v /homes/username1/output:output outputter 使用root作为所有者创建文件. 我需要在Dockerfile或启动选项

在组“usergroup1”中以名称“username1”的用户启动docker容器时.

并且该容器在本地文件系统上具有卷的文件/文件夹:
例如.

$username1>docker run -v /homes/username1/output:output outputter

使用root作为所有者创建文件.

我需要在Dockerfile或启动选项中做些什么来确保输出文件夹中的文件rigth与localuser:group相同,在这种情况下username1:usergroup1?

最佳答案
如this project中所述:

By default,our docker containers run as the root user. Files created or modified by the container will thus become owned by the root user,even after quitting the container.
To avoid this problem,it is necessary to run the container using a non-root user.

If the host machine user has a UID other than 1000 (or 0,for root),the user should specify their UID when running docker,e.g.

docker run -d -P -v $(pwd):/home/$USER/foo 
  -e USER=$USER  -e USERID=$UID rocker/rstudio

to avoid changing the permissions in the linked volume on the host

这是有效的,因为project Dockerfile,当启动容器时,creates a user with the same uid(名称并不重要)

## (Docker cares only about uid,not username; diff users with same uid = confusion)
if [ "$USERID" -ne 1000 ]
## Configure user with a different USERID if requested.
    then
        echo "creating new $USER with UID $USERID"
        useradd -m $USER -u $USERID
        mkdir /home/$USER
        chown -R $USER /home/$USER

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读