docker-entrypoint.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/bin/bash
  2. # Get the PUID and PGID from environment variables (or use default values 1000 if not set)
  3. PUID=${PUID:-1000}
  4. PGID=${PGID:-1000}
  5. # Check if the provided PUID and PGID are non-empty, numeric values; otherwise, assign default values.
  6. if ! [[ "$PUID" =~ ^[0-9]+$ ]]; then
  7. PUID=1000
  8. fi
  9. if ! [[ "$PGID" =~ ^[0-9]+$ ]]; then
  10. PGID=1000
  11. fi
  12. # Check if the specified group with PGID exists, if not, create it.
  13. if ! getent group "$PGID" >/dev/null; then
  14. groupadd -g "$PGID" appgroup
  15. fi
  16. # Create user if it doesn't exist.
  17. if ! getent passwd "$PUID" >/dev/null; then
  18. useradd --create-home --shell /bin/bash --uid "$PUID" --gid "$PGID" appuser
  19. fi
  20. # Make user the owner of the app directory.
  21. chown -R "$PUID":"$PGID" /app
  22. # Copy the default .bashrc file to the appuser home directory.
  23. cp /etc/skel/.bashrc /home/appuser/.bashrc
  24. chown "$PUID":"$PGID" /home/appuser/.bashrc
  25. export HOME=/home/appuser
  26. # Set permissions on font directories.
  27. if [ -d "/usr/share/fonts" ]; then
  28. chmod -R 777 /usr/share/fonts
  29. fi
  30. if [ -d "/var/cache/fontconfig" ]; then
  31. chmod -R 777 /var/cache/fontconfig
  32. fi
  33. if [ -d "/usr/local/share/fonts" ]; then
  34. chmod -R 777 /usr/local/share/fonts
  35. fi
  36. export PATH=/home/appuser/.local/bin:$PATH
  37. # Switch to appuser and execute the Docker CMD or passed in command-line arguments.
  38. # Using setpriv let's it run as PID 1 which is required for proper signal handling (similar to gosu/su-exec).
  39. exec setpriv --reuid=$PUID --regid=$PGID --init-groups $@