tiredofit / docker-nginx

Dockerized webserver with many customizable options
MIT License
25 stars 10 forks source link

Support specifying uid and gid #23

Open coolibre opened 2 months ago

coolibre commented 2 months ago

Description of the feature I need the possibility to dynamically specify the uid and gid of the nginx user e.g. NGINX_USER_UID=50000 and NGINX_GROUP_GID=500000.

Benftits of feature streamlined configuration option as NGINX_USER is already available

Additional context In a AWS EKS scenario I have the problem that freescout tries to modify the permissions of a volume which is based on CSI EFS dynamic provisioning.

When tiredofit/docker-freescout starts, one startup script tries to "chown -R /data nginx:www-data" which is reported with "operation not permitted" due to fixed uid gid in the AWS EKS CSI storage driver in combination with the PersistentVolumeClaim of the freescout container. So essentially the permission to chown the /data directory is only allowed for a user or group with id 50000 (specified during EFS provisioning).

As tracked here a workaround is to set the uid and gid of the user using the volume to the ones specified in the provisioning, nothing else works: https://github.com/kubernetes-sigs/aws-efs-csi-driver/issues/300#issuecomment-943683371

My workaround now is to change the uid and guid from a cont-init.d script, mounted into /etc/cont-init.d, running before the nginx setup. This seems promising and freescout starts without any error.

apiVersion: v1
kind: ConfigMap
metadata:
  name: freescout-efs
data:
  08-change-nginx-uid-www-data-gid: |
    #!/command/with-contenv bash
    # Change UID and GID of www-data to 50000 to be able to access EFS
    sed -i 's/www-data:x:82:nginx/www-data:x:50000:nginx/' /etc/group
    sed -i 's/nginx:x:80:82:nginx:/nginx:x:50000:50000:nginx:/' /etc/passwd
    # adapt the rights of the files
    find / -user 80 -exec chown -h 50000 {} \;
    find / -group 82 -exec chgrp -h 50000 {} \;
    echo "AWS EFS HACK: nginx UID and www-data GID changed to 50000"
    # this is needed to inform tiredofit containers that this step is done
    source /assets/functions/00-container
    liftoff

Log before:

                                                                       ,---.
,--------.,--.                 ,--.            ,---.    ,--. ,--------.|   |
'--.  .--'`--',--.--. ,---.  ,-|  |     ,---. /  .-'    |  | '--.  .--'|  .'
   |  |   ,--.|  .--'| .-. :' .-. |    | .-. ||  `-,    |  |    |  |   |  |
   |  |   |  ||  |   \   --.\ `-' |    ' '-' '|  .-'    |  |.--.|  |   `--'
   `--'   `--'`--'    `----' `---'      `---' `--'      `--''--'`--'   .--.
                                                                       '--'
Image:  tiredofit/freescout | Version  1.17.61 Type 'image_changelog' for details
Repository/Documentation: https://github.com/tiredofit/docker-freescout/

If this image provides you value  - Consider sponsoring my work for continued 
development, timely updates, and feature requests. Commercial support available.

                    More Info:  https://www.tiredofit.ca

2024-08-21.09:36:51 [NOTICE] ** [monitoring] Container configured for monitoring with 'zabbix modern'
2024-08-21.09:36:51 [NOTICE] ** [scheduling] Container configured for scheduled tasks with 'cron'
2024-08-21.09:36:52 [NOTICE] ** [messaging] Container configured to route mail via SMTP to ******
2024-08-21.09:36:55 [NOTICE] ** [php-fpm] Creating sample index.php
2024-08-21.09:36:59 [WARN] ** [freescout] Potential New Installation / Standalone Installation Detected - Copying Freescout Sourcecode
2024-08-21.09:37:00 [WARN] ** [freescout] Detected /data directory. Persistently saving settings/sessions/cache/uploads/modules
chown: /www/html/.env: Operation not permitted
chown: /data/config: Operation not permitted
chown: /data/.freescout-version: Operation not permitted
chown: /data/Modules/.gitkeep: Operation not permitted
chown: /data/Modules: Operation not permitted
chown: /data/Modules: Operation not permitted
chown: /data/storage/debugbar/.gitignore: Operation not permitted
chown: /data/storage/debugbar: Operation not permitted
chown: /data/storage/debugbar: Operation not permitted
chown: /data/storage/framework/cache/data/71/e7/71e745d13645beba997b5011324db8ee58db0e30: Operation not permitted
chown: /data/storage/framework/cache/data/71/e7: Operation not permitted
chown: /data/storage/framework/cache/data/71/e7: Operation not permitted
chown: /data/storage/framework/cache/data/71: Operation not permitted
chown: /data/storage/framework/cache/data/71: Operation not permitted
chown: /data/storage/framework/cache/data/ac/3b: Operation not permitted
chown: /data/storage/framework/cache/data/ac/3b: Operation not permitted
chown: /data/storage/framework/cache/data/ac: Operation not permitted
chown: /data/storage/framework/cache/data/ac: Operation not permitted
chown: /data/storage/framework/cache/data/fd/78: Operation not permitted
chown: /data/storage/framework/cache/data/fd/78: Operation not permitted
chown: /data/storage/framework/cache/data/fd: Operation not permitted
chown: /data/storage/framework/cache/data/fd: Operation not permitted
chown: /data/storage/framework/cache/data/5e/3c/5e3cbbed47195384f39edf30b47d8cd8245cdff8: Operation not permitted
chown: /data/storage/framework/cache/data/5e/3c: Operation not permitted
chown: /data/storage/framework/cache/data/5e/3c: Operation not permitted
chown: /data/storage/framework/cache/data/5e: Operation not permitted
chown: /data/storage/framework/cache/data/5e: Operation not permitted
chown: /data/storage/framework/cache/data/ee/2f/ee2f842aa7bb1f53edf3a2ed2c09a1807ffa6c90: Operation not permitted
chown: /data/storage/framework/cache/data/ee/2f: Operation not permitted
chown: /data/storage/framework/cache/data/ee/2f: Operation not permitted
chown: /data/storage/framework/cache/data/ee: Operation not permitted
chown: /data/storage/framework/cache/data/ee: Operation not permitted
chown: /data/storage/framework/cache/data/5d/5a: Operation not permitted
chown: /data/storage/framework/cache/data/5d/5a: Operation not permitted
chown: /data/storage/framework/cache/data/5d: Operation not permitted
chown: /data/storage/framework/cache/data/5d: Operation not permitted
chown: /data/storage/framework/cache/data/f3/26/f326919d37ba59158e4fd05f8f54ae22a4659438: Operation not permitted
chown: /data/storage/framework/cache/data/f3/26: Operation not permitted
chown: /data/storage/framework/cache/data/f3/26: Operation not permitted
chown: /data/storage/framework/cache/data/f3: Operation not permitted
chown: /data/storage/framework/cache/data/f3: Operation not permitted
chown: /data/storage/framework/cache/data/6a/53: Operation not permitted
chown: /data/storage/framework/cache/data/6a/53: Operation not permitted
chown: /data/storage/framework/cache/data/6a: Operation not permitted
chown: /data/storage/framework/cache/data/6a: Operation not permitted
chown: /data/storage/framework/cache/data: Operation not permitted
chown: /data/storage/framework/cache/data: Operation not permitted
chown: /data/storage/framework/cache/.gitignore: Operation not permitted
chown: /data/storage/framework/cache: Operation not permitted
chown: /data/storage/framework/cache: Operation not permitted
chown: /data/storage/framework/.gitignore: Operation not permitted
chown: /data/storage/framework/testing/.gitignore: Operation not permitted
chown: /data/storage/framework/testing: Operation not permitted
chown: /data/storage/framework/testing: Operation not permitted
chown: /data/storage/framework/views/.gitignore: Operation not permitted
chown: /data/storage/framework/views/526f29c52b6e5b7df3292f62e08363cb0ebfbb03.php: Operation not permitted
chown: /data/storage/framework/views: Operation not permitted
chown: /data/storage/framework/views: Operation not permitted
chown: /data/storage/framework/sessions/.gitignore: Operation not permitted
chown: /data/storage/framework/sessions: Operation not permitted
chown: /data/storage/framework/sessions: Operation not permitted
chown: /data/storage/framework: Operation not permitted
chown: /data/storage/framework: Operation not permitted
chown: /data/storage/app/.gitignore: Operation not permitted
chown: /data/storage/app/public/.htaccess: Operation not permitted
chown: /data/storage/app/public/.gitignore: Operation not permitted
chown: /data/storage/app/public/js/vars.js: Operation not permitted
chown: /data/storage/app/public/js: Operation not permitted
chown: /data/storage/app/public/js: Operation not permitted
chown: /data/storage/app/public: Operation not permitted
chown: /data/storage/app/public: Operation not permitted
chown: /data/storage/app: Operation not permitted
chown: /data/storage/app: Operation not permitted
chown: /data/storage/logs: Operation not permitted
chown: /data/storage: Operation not permitted
chown: /data/storage: Operation not permitted
chown: /data: Operation not permitted
chown: /data: Operation not permitted
2024-08-21.09:37:02 [INFO] ** [freescout] Auto Configuring based on Environment Variables
chown: /data/config: Operation not permitted
2024-08-21.09:37:06 [WARN] ** [freescout] Installing Modules into container
2024-08-21.09:37:07 [WARN] ** [freescout] Running any Database Migrations

Workaround log:

                                                                       ,---.
,--------.,--.                 ,--.            ,---.    ,--. ,--------.|   |
'--.  .--'`--',--.--. ,---.  ,-|  |     ,---. /  .-'    |  | '--.  .--'|  .'
   |  |   ,--.|  .--'| .-. :' .-. |    | .-. ||  `-,    |  |    |  |   |  |
   |  |   |  ||  |   \   --.\ `-' |    ' '-' '|  .-'    |  |.--.|  |   `--'
   `--'   `--'`--'    `----' `---'      `---' `--'      `--''--'`--'   .--.
                                                                       '--'
Image:  tiredofit/freescout | Version  1.17.61 Type 'image_changelog' for details
Repository/Documentation: https://github.com/tiredofit/docker-freescout/

If this image provides you value  - Consider sponsoring my work for continued 
development, timely updates, and feature requests. Commercial support available.

                    More Info:  https://www.tiredofit.ca

2024-08-21.09:31:28 [NOTICE] ** [monitoring] Container configured for monitoring with 'zabbix modern'
2024-08-21.09:31:28 [NOTICE] ** [scheduling] Container configured for scheduled tasks with 'cron'
2024-08-21.09:31:28 [NOTICE] ** [messaging] Container configured to route mail via SMTP to *****
AWS EFS HACK: nginx UID and www-data GID changed to 50000
2024-08-21.09:31:55 [NOTICE] ** [php-fpm] Creating sample index.php
2024-08-21.09:31:56 [WARN] ** [freescout] Potential New Installation / Standalone Installation Detected - Copying Freescout Sourcecode
2024-08-21.09:31:57 [WARN] ** [freescout] Detected /data directory. Persistently saving settings/sessions/cache/uploads/modules
2024-08-21.09:31:57 [INFO] ** [freescout] Auto Configuring based on Environment Variables
2024-08-21.09:31:59 [WARN] ** [freescout] Installing Modules into container
2024-08-21.09:31:59 [WARN] ** [freescout] Running any Database Migrations
2024-08-21.09:32:01 [INFO] ** [freescout] Initialization Complete. Please visit ******* and login with your credentials..
2024-08-21.09:32:01 [STARTING] ** [nginx] [1] Starting nginx 1.25.4
2024-08-21.09:32:01 [STARTING] ** [php-fpm] [1] Starting php-fpm 8.2.17
2024-08-21.09:32:01 [STARTING] ** [monitoring] [1] Starting Zabbix Agent (modern) 6.4.13
2024-08-21.09:32:01 [STARTING] ** [scheduling] [1] Starting cron
tiredofit commented 2 months ago

Hi, buried in the documentation of the base image I support changing permissions for this exact use case. Have a peek here:

tiredofit/docker-alpine - Permissions

coolibre commented 2 months ago

Hi, thank you very much I didnt find that and it seems indeed very useful for me.

The uid of the nginx user is changing with USER_NGINX=50000. Unfortunately the gid update is not working in this exact case I need as the script seems to have problems with the "-" in the groupname "WWW-DATA". The GROUP_WWW-DATA=50000 or GROUP_www-data=50000 is never replaced nor shown in the logs. Wheras when i experiment with GROUP_POSTGRES=50000 it works perfectly.

negative examples (GROUP_WWW-DATA and GROUP_www-data):

                                                                       ,---.
,--------.,--.                 ,--.            ,---.    ,--. ,--------.|   |
'--.  .--'`--',--.--. ,---.  ,-|  |     ,---. /  .-'    |  | '--.  .--'|  .'
   |  |   ,--.|  .--'| .-. :' .-. |    | .-. ||  `-,    |  |    |  |   |  |
   |  |   |  ||  |   \   --.\ `-' |    ' '-' '|  .-'    |  |.--.|  |   `--'
   `--'   `--'`--'    `----' `---'      `---' `--'      `--''--'`--'   .--.
                                                                       '--'
Image:  tiredofit/freescout | Version  1.17.61 Type 'image_changelog' for details
Repository/Documentation: https://github.com/tiredofit/docker-freescout/

If this image provides you value  - Consider sponsoring my work for continued 
development, timely updates, and feature requests. Commercial support available.

                    More Info:  https://www.tiredofit.ca

2024-08-22.09:17:46 [DEBUG] ** [permissions] Enabling permissions features
2024-08-22.09:17:46 [DEBUG] ** [permissions] Changing user 'nginx' to uid '50000' from '80'
2024-08-22.09:17:46 [NOTICE] ** [monitoring] Container configured for monitoring with 'zabbix modern'

positive GROUP_POSTGRES

                                                                       ,---.
,--------.,--.                 ,--.            ,---.    ,--. ,--------.|   |
'--.  .--'`--',--.--. ,---.  ,-|  |     ,---. /  .-'    |  | '--.  .--'|  .'
   |  |   ,--.|  .--'| .-. :' .-. |    | .-. ||  `-,    |  |    |  |   |  |
   |  |   |  ||  |   \   --.\ `-' |    ' '-' '|  .-'    |  |.--.|  |   `--'
   `--'   `--'`--'    `----' `---'      `---' `--'      `--''--'`--'   .--.
                                                                       '--'
Image:  tiredofit/freescout | Version  1.17.61 Type 'image_changelog' for details
Repository/Documentation: https://github.com/tiredofit/docker-freescout/

If this image provides you value  - Consider sponsoring my work for continued 
development, timely updates, and feature requests. Commercial support available.

                    More Info:  https://www.tiredofit.ca

2024-08-22.09:06:59 [DEBUG] ** [permissions] Enabling permissions features
2024-08-22.09:06:59 [DEBUG] ** [permissions] Changing user 'nginx' to uid '50000' from '80'
2024-08-22.09:06:59 [DEBUG] ** [permissions] Changing group 'postgres' to gid '50000' from '70'
2024-08-22.09:06:59 [NOTICE] ** [monitoring] Container configured for monitoring with 'zabbix modern'
...
tiredofit commented 2 months ago

Interesting. docker run -it -e DEBUG_PERMISSIONS=TRUE -e CONTAINER_GROUP_WWW_DATA=80 tiredofit/nginx:alpine-3.20 gives the following extra output:

2024-08-22.14:23:12 [DEBUG] ** [permissions] Enabling permissions features
2024-08-22.14:23:12 [DEBUG] ** [permissions] Changing group 'www_data' to gid '80' from ''

Changing CONTAINER_GROUP_WWW_DATA -> CONTAINER_GROUP_WWW-DATA creates zero ouput as does CONTAINER_GROUP_WWWDATA.

Definitely the hyphen is breaking the group gid change procedure. Surprised this is coming up after 6 years (!) after introducing, but I am not at the same time.

I have a new version of all the base images which actually drop www-data and switch to a unified uid / gid that I have been sitting on for a couple of years it seems now and slowly inching towards release which would resolve. I'll see if I have time in near future to look at existing routines and issue a patch to accomodate for names with hyphens/-/ in them.

tiredofit commented 2 months ago

Problem is fixed here: https://github.com/tiredofit/docker-alpine/commit/1bfa4149f12705a9e98958cb736860530759f9e2 - I'll go ahead and run a rebuild of :latest for this image although it won't be a new versioned release.