Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run php:fpm as non-root #70

Closed
md5 opened this issue Feb 21, 2015 · 14 comments
Closed

Run php:fpm as non-root #70

md5 opened this issue Feb 21, 2015 · 14 comments

Comments

@md5
Copy link
Contributor

md5 commented Feb 21, 2015

Unlike the php:apache image where Apache drops root privileges to www-data before running any PHP code, the php:fpm image is still running as root.

Since it doesn't actually need root privileges, it would probably be best if php:fpm ran PHP code as a non-root user. In the case of php:fpm, it seems like it should work fine to use a USER fpm without pulling in gosu or anything like that.

@md5
Copy link
Contributor Author

md5 commented Feb 21, 2015

To accomodate switching between php:apache-based images and php:fpm-based images, it may be best to run php-fpm as www-data with the same uid as the php:apache image.

Then again, it would also be nice to be able to easily switch php:apache-based images to a user other than www-data I know I did some testing with switching the user that apache2 uses by having it pick up the APACHE_RUN_USER environment variable, but I can't for the life of me find the branch or gist (maybe it was just a comment).

In the case of php:fpm, I think that switching should be possible by using -u myuser as opposed to things like APACHE_RUN_USER.

@md5
Copy link
Contributor Author

md5 commented Feb 21, 2015

Derp:

ENV PHP_EXTRA_CONFIGURE_ARGS --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data

Looks like it already runs as www-data. Sorry for the false alarm!

@md5 md5 closed this as completed Feb 21, 2015
@sagikazarmark
Copy link

Actually, I would rather reopen this as the master process still runs as root:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 174384 18104 ?        Ss   11:02   0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
www-data     9  0.0  0.1 185184 24516 ?        S    11:02   0:00 php-fpm: pool www
www-data    10  0.0  0.1 190832 29812 ?        S    11:02   0:00 php-fpm: pool www

@yosifkit
Copy link
Member

yosifkit commented Aug 4, 2016

@sagikazarmark as far as I can tell that is identical to many installs on non-container systems running apache/php or fpm:

# from my system with host installed apache/php
$ ps aux | grep apache
root      2814  0.0  0.0 267996 16348 ?        Ss   Jul12   1:13 /usr/sbin/apache2 -D INFO -D LANGUAGE -D PHP5 -D PROXY -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start
apache    3952  0.0  0.0 269956 18288 ?        S    Aug01   0:00 /usr/sbin/apache2 -D INFO -D LANGUAGE -D PHP5 -D PROXY -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start
apache    3970  0.0  0.0 270056 17640 ?        S    Aug01   0:00 /usr/sbin/apache2 -D INFO -D LANGUAGE -D PHP5 -D PROXY -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start
apache    8069  0.0  0.0 197788  7088 ?        S    Jul31   0:00 /usr/sbin/apache2 -D INFO -D LANGUAGE -D PHP5 -D PROXY -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start
apache    8172  0.0  0.0 270344 18732 ?        S    Jul31   0:00 /usr/sbin/apache2 -D INFO -D LANGUAGE -D PHP5 -D PROXY -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start
apache    8173  0.0  0.0 269916 17868 ?        S    Jul31   0:00 /usr/sbin/apache2 -D INFO -D LANGUAGE -D PHP5 -D PROXY -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start
apache    8175  0.0  0.0 269888 17652 ?        S    Jul31   0:00 /usr/sbin/apache2 -D INFO -D LANGUAGE -D PHP5 -D PROXY -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start
apache    8176  0.0  0.0 269964 17744 ?        S    Jul31   0:00 /usr/sbin/apache2 -D INFO -D LANGUAGE -D PHP5 -D PROXY -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start
apache   11306  0.0  0.0 270084 17584 ?        S    Aug03   0:00 /usr/sbin/apache2 -D INFO -D LANGUAGE -D PHP5 -D PROXY -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start
apache   16977  0.0  0.0 269120 16780 ?        S    Aug02   0:00 /usr/sbin/apache2 -D INFO -D LANGUAGE -D PHP5 -D PROXY -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start
apache   24479  0.0  0.0 269572 17996 ?        S    Aug03   0:00 /usr/sbin/apache2 -D INFO -D LANGUAGE -D PHP5 -D PROXY -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start
apache   27083  0.0  0.0 270056 17764 ?        S    Aug02   0:00 /usr/sbin/apache2 -D INFO -D LANGUAGE -D PHP5 -D PROXY -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start

@tianon
Copy link
Member

tianon commented Aug 4, 2016

and can be overcome by using --user www-data, if full-coverage is necessary:

$ dockr run -it --rm --user www-data php:fpm
[04-Aug-2016 18:09:04] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[04-Aug-2016 18:09:04] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
[04-Aug-2016 18:09:04] NOTICE: fpm is running, pid 1
[04-Aug-2016 18:09:04] NOTICE: ready to handle connections

@sagikazarmark
Copy link

Indeed. Actually I am not sure about this, because on side there is the security issue of running root inside the container, on the other side there is what you say. So what's the correct way of doing this? I read about security issues and recommendations to run processes with non-root UID.

@tianon
Copy link
Member

tianon commented Aug 4, 2016 via email

@sagikazarmark
Copy link

Great, thanks @tianon

@ibotty
Copy link

ibotty commented Jun 10, 2018

When running as root, it cannot run on many container installations, because some do only allow running non-root containers. I don't see the problem that could arise when running as www-data by default. Is there any drawback?

@tianon
Copy link
Member

tianon commented Jun 11, 2018

As it's written, the container will start as root and step down to www-data automatically (which is the same pattern most official images use). If you have stronger constraints than that, there shouldn't be anything preventing Docker's --user flag from working out-of-the-box.

@ibotty
Copy link

ibotty commented Jun 12, 2018

Unfortunately that makes running this image on openshift online harder than it needs to be. Kubernetes (which is the basis of openshift) will automatically try to run the image with the configured USER. That won't be allowed in some configurations and will need additional non-obvious configuration to specify the user to use.

I don't really see what the problem with running as www-data by default is. Is there any advantage in running as root?

@tianon
Copy link
Member

tianon commented Jun 12, 2018 via email

@flylan
Copy link

flylan commented Jan 18, 2023

As far as I know, the running mode of php-fpm is the leader follow model. It does not need tools like gosu. You can change the users of the php-fpm working process through the php-fpm-user and php-fpm-group parameters. In addition, when installing php-fpm on traditional servers, the management process is root, and the working process is run by other users, such as www.

Running the php-fpm management process directly as a non-root user will cause many problems, such as the inability to create a sock file and port binding

@flylan
Copy link

flylan commented Jan 18, 2023

For the software running in the leader follow model, the management process only does the management work, unlike the working process, which will run the user code, so that the management process is safe, so it is safe to run as root user. The working process is different, because the working process will run the code of the user end, and the code of the user end may have vulnerabilities exploited by hackers. Some vulnerabilities can allow hackers to hack into the system, so it is necessary to run the working process as a non-root user, so even if the hacker hacks into the system, it can only be accessed as an ordinary user

@docker-library docker-library locked as resolved and limited conversation to collaborators Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants