linux - "usermod: UID ' 0 ' already exists"为什么?

标签 linux bash docker

作为我的 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 重建了图像。 所以没有使用缓存)

最佳答案

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

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

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

(我不确定在任何情况下将 www-data 的 uid 更改为 1000 是否是个好主意,您必须已经拥有 usermod 的用户能够更改其 uid。但这不是问题所在。)

bash :

UID
Expands to the user ID of the current user, initialized at shell startup. This variable is readonly.

用户模式:

-u, --uid UID
The new numerical value of the user's ID.
This value must be unique, unless the -o option is used. The value must be non-negative.

关于linux - "usermod: UID ' 0 ' already exists"为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44592372/

相关文章:

linux - 如何安装特定版本的postgres?

java - 如何用java编写文件搜索程序?

regex - bash 获取最多 2 位数的正则表达式编号

linux - 使用wget获取谷歌翻译的结果

java - bash中如何模拟ctrl+c复制文件

docker - 没有负载平衡器的暴露容器

linux - 将 xls 文件转换为 xlsx 文件的 Unix 命令?

mysql - Docker(docker-compose) Node +MySQL : ECONNREFUSED

docker - 如何在 Dockerfile CMD 中使用变量?

linux - 使用 awk 或 sed 从多个文件中消除重复行