I have the following script as the ENTRYPOINT of my Dockerfile and therefore Docker image:
#!/bin/bash
set -e
# Setup permissions
data_dir="/var/www/html"
usermod -u 1000 www-data && groupmod -g 1000 www-data
chown -R www-data:root "$data_dir"
if [ -d "$data_dir" ]; then
chgrp -R 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
# Enable rewrite
a2enmod rewrite expires
# Apache gets grumpy about PID files pre-existing
rm -f /var/run/apache2/apache2.pid
source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND "$@"
Everything is running fine for Linux since the GUI and UID for most of the distros is 1000
(we're using Fedora and Ubuntu). Windows - I think - doesn't care about it, but again the script works properly and everything goes well.
The problem comes when I try to run this in Mac (OSX) since the GUI and UID for the first user is 500
. That makes the permissions not to work properly.
I know I can always change the values from 500 to 1000 but ....
Is there any way to get this from inside the script so this is transparent for the user?
UPDATE
AS per the answer below, this is how my script looks like:
#!/bin/bash
set -e
# Setup permissions
data_dir="/var/www/html"
usermod -u ${UID} www-data && groupmod -g ${GUID} 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
# Enable rewrite
a2enmod rewrite expires
# Apache gets grumpy about PID files pre-existing
rm -f /var/run/apache2/apache2.pid
source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND "$@"
What would happen if I hasn't defined the UID
or GUID
? Is there any way to rely on default values as 1000:1000
(first created user)?