【问题标题】:"usermod: UID '0' already exists" why?“usermod:UID'0'已经存在”为什么?
【发布时间】:2017-06-16 14:58:10
【问题描述】:

作为我的 Docker 映像的一部分,我有以下 ENTRYPOINT 脚本:

#!/bin/bash
set -e

if [ -z "${UID}" ]; then
    uid=1000;
else
    uid=${UID};
fi

if [ -z "${GID}" ]; then
    gid=1000;
else
    gid=${GID};
fi

echo "UID: $uid"
echo "GID: $gid"

data_dir="/var/www/html"
usermod -u "$uid" www-data && groupmod -g "$gid" www-data
chown -R www-data:root "$data_dir"

if  [ -d "$data_dir" ]; then
    chgrp -RH www-data "$data_dir"
    chmod -R g+w "$data_dir"
    find "$data_dir" -type d -exec chmod 2775 {} +
    find "$data_dir" -type f -exec chmod ug+rw {} +
fi

composer_cache_dir="/var/www/.composer"
mkdir -p "$composer_cache_dir"
chown -R www-data:root "$composer_cache_dir"

if  [ -d "$composer_cache_dir" ]; then
    chgrp -R www-data "$composer_cache_dir"
    chmod -R g+w "$composer_cache_dir"
fi

a2enmod rewrite expires

rm -f /var/run/apache2/apache2.pid

source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND "$@"

镜像编译成功。当我尝试通过运行以下命令docker run -it temp bash 来运行容器时,我得到了以下输出:

UID: 0
GID: 1000
usermod: UID '0' already exists
Enabling module rewrite.
Enabling module expires.
To activate the new configuration, you need to run:
  service apache2 restart
AH00112: Warning: DocumentRoot [/var/www/html/public_html] does not exist
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message

为什么我的UID0?我在这里找不到我的错误,任何帮助都非常受欢迎。

更新

在脚本上更改这一行:

if [ "$UID" != 0 ]; then
    uid=1000;
else
    uid=${UID};
fi

仍然将0 分配给uid,是因为var 名称吗? (我已经使用docker build --no-cache -t temp . 重建了图像,所以没有使用缓存)

【问题讨论】:

    标签: linux bash docker


    【解决方案1】:

    因为 Bash 中的 $UID 扩展为 shell 的当前用户 ID,并且它永远不会为空,所以 [ -z "${UID}" ] 永远不会为真。因此,如果脚本以 root 身份运行,您将设置 uid=${UID};,可能是 uid=0

    然后usermod -u 0 www-data 抱怨,因为它试图将www-data 的UID 更改为零,但这已经在使用中,并且没有-o 标志它不会这样做。 (而且你无论如何都不想这样做......)

    如果您想测试$UID 是否为零,请使用[ "$UID" == 0 ]。或 [[ $UID == 0 ]] 在 Bash 中。或[ "$UID" -eq 0 ] 进行数字比较,这并不重要。

    (我不确定将 www-data 的 uid 更改为 1000 是否是个好主意,您必须拥有 usermod 的用户才能更改其 uid。但那不是问题。)

    重击:

    UID
    展开为当前用户的用户 ID,在 shell 启动时初始化。这个变量是只读的。

    用户模式:

    -u, --uid UID
    用户 ID 的新数值。
    该值必须是唯一的,除非使用了 -o 选项。该值必须为非负数。

    【讨论】:

    • 我对检查做了一些小改动,但0 仍然分配给uid 不知道为什么,你能看看吗?由于权限,我需要更改www-dataUID。这是 Docker,有一个主机目录映射为卷,如果运行 Docker 的用户的 UID1000,因为它适用于第一个操作系统用户,而 www-dataUID 在 Docker 容器中是不同的那么我需要在我身边交换它们,我更喜欢在容器中进行。
    • 这个想法来自here 这就是我在这里想要实现的目标
    • @ReynierPM,bfft,我把比较弄错了。你可能想要if [ "$UID" = 0 ] ; then uid=1000 ...
    • 谢谢,这是我之前没看到的问题
    猜你喜欢
    • 2021-08-12
    • 2021-12-20
    • 2018-10-21
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 2020-11-28
    相关资源
    最近更新 更多