“usermod:UID'0'already exists”why?

作为我的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 ? 我无法find我的错误在哪里,任何帮助都是值得欢迎的。

更新

在脚本中更改这些行:

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

还是把它分配给uid ,是因为var的名字? (我用docker build --no-cache -t temp .重build了镜像,所以没有使用caching)

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

然后usermod -u 0 www-data抱怨,因为它试图将www-data的UID更改为零,但是这已经在使用了,而且没有-o标志就没有这个function。 (而且你永远不想这样做…)

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

(我不确定是否将www-data的uid更改为1000是一个好主意,您已经必须让usermod用户能够更改它的uid,但这不是问题)。

击:

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

usermod命令:

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