简体   繁体   中英

How to run php-fpm as root in docker

When i run php-fpm in docker,the default account is www-data,but i want run as root in docker.

I edit the config file,user and user group to root,but the I don't know how to add -R.so the container can't run.

Here is how you can run bash as a root user:

First, find docker container id for php-fpm:

$ docker ps

p, 0.0.0.0:8008->80/tcp   webserver
e4689b3c1bc5        php:7.2-fpm                            "docker-php-entrypoi…"   2 months ago        Up 41 hours         9000/tcp   

Then run the following command:

$ docker exec -u 0 -it e4689b3c1bc5 bash

Then you can run bash as a root user:

root@e4689b3c1bc5:/var/www# 

If you check the default PHP-FPM container command, you will see that it uses the following default one: php-fpm . You can easily override it to php-fpm -R .

For docker-compose , I'm using the following config:

services:
  fpm:
    image: php:7.3-fpm
    volumes:
      - ./zzz-docker.conf:/usr/local/etc/php-fpm.d/zzz-docker.conf # Mount FPM config that runs FPM as root
    command: php-fpm -R # Allow running FPM as root

The config file is pretty simple:

; Config to run PHP FPM as root (requires the -R flag to be passed to the entrypoint)
[www]
user = root
group = root

Instead of mounting custom config and changing the command, you may build a custom image adding this config file to your Dockerfile and it will look like the following:

FROM php:7.3-fpm

COPY ./zzz-docker.conf /usr/local/etc/php-fpm.d/zzz-docker.conf

CMD ["php-fpm", "-R"]

The zzz-docker.conf file in these examples is located in the root directory of the project.

Remember that you should aware of the security risks of running PHP-FPM as root. I'm using the above config for development purposes only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM