Skip to content
Advertisement

“usermod: UID ‘0’ already exists” why?

As part of my Docker image I have the following ENTRYPOINT script:

#!/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 "$@"

The image compiles successfully. When I try to run the container by running the following command docker run -it temp bash I got the following output:

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

Why my UID is 0? I can’t find where is my error here, any help is more than welcome.

Update:

Changing this lines on the script:

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

Still making it to assign 0 to uid, is because of the var name? (I have rebuilt the image using docker build --no-cache -t temp . so no cache is being used)

Advertisement

Answer

Because $UID in Bash expands to the current user id of the shell, and it’s never empty, so [ -z "${UID}" ] is never true. So, you’re setting uid=${UID};, probably uid=0 if the script runs as root.

Then usermod -u 0 www-data complains because it tries to change the UID of www-data to zero, but that’s already in use, and it doesn’t do that without the -o flag. (And you’d never want to do that anyway…)

If you want to test if $UID is zero, use [ "$UID" == 0 ]. Or [[ $UID == 0 ]] in Bash. Or [ "$UID" -eq 0 ] for a numeric comparison, not that it matters.

(I’m not exactly sure if changing the uid of www-data to 1000 is a good idea in any case, you already must have the user for usermod to be able to change its uid. But that wasn’t the question.)

Bash:

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

usermod:

-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.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement