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

前端之家收集整理的这篇文章主要介绍了如何创建与主机用户具有相同文件权限的docker镜像/容器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在组“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
原文链接:https://www.f2er.com/docker/436180.html

猜你在找的Docker相关文章