Skip to content

Instantly share code, notes, and snippets.

@guillemcanal
Created August 31, 2016 10:14
Show Gist options
  • Save guillemcanal/818c0b562a2d10d7cafda800a8afbf54 to your computer and use it in GitHub Desktop.
Save guillemcanal/818c0b562a2d10d7cafda800a8afbf54 to your computer and use it in GitHub Desktop.
docker run apache from api plateform
#!/bin/bash
##
# Detect the ownership of the webroot
# and run apache as that user.
#
main() {
# Docker for Mac requires a special handling, see https://docs.docker.com/docker-for-mac/osxfs/#/ownership
if [[ -n $(mount -t fuse.osxfs | grep /app) ]]; then
chown -R www-data:www-data /app 2> /dev/null
exec /usr/sbin/apache2ctl "$@"
return
fi
local owner group owner_id group_id tmp
read owner group owner_id group_id < <(stat -c '%U %G %u %g' .)
if [[ $owner = UNKNOWN ]]; then
owner=$(randname)
if [[ $group = UNKNOWN ]]; then
group=$owner
addgroup --system --gid "$group_id" "$group"
fi
adduser --system --uid=$owner_id --gid=$group_id "$owner"
fi
tmp=/tmp/$RANDOM
{
echo "User $owner"
echo "Group $group"
grep -v '^User' /etc/apache2/apache2.conf |
grep -v '^Group'
} >> "$tmp" &&
cat "$tmp" > /etc/apache2/apache2.conf &&
rm "$tmp"
# Not volumes, so need to be chowned
chown -R "$owner:$group" /var/{lock,log,run}/apache*
exec /usr/sbin/apache2ctl "$@"
}
##
# Generate a random sixteen-character
# string of alphabetical characters
randname() {
local -x LC_ALL=C
tr -dc '[:lower:]' < /dev/urandom |
dd count=1 bs=16 2>/dev/null
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment