In a multiple-container Docker application deployed with docker-compose I have a container built from alpine running PHP-FPM. That container is used both for providing PHP to the Nginx container, but also to run PHP scripts on the container from a CLI command on the host. Files created by PHP when run from CLI commands on the host are owned by root (UID 0), and have the root group (GID 0), even though FPM is configured to use the user www-data (UID 1000) and group www-data (GID 1000).

Here are the relevant parts of the Dockerfile:

FROM php:7.4-fpm-alpine
RUN echo http://dl-2.alpinelinux.org/alpine/edge/community/ >> /etc/apk/repositories
RUN apk --no-cache add shadow && usermod -u 1000 www-data && groupmod -g 1000 www-data
RUN chown -R www-data:www-data /var/www/html

Here is the configuration:

$ docker-compose exec php grep -E "user|group" /usr/local/etc/php-fpm.d/www.conf
user = www-data
group = www-data

$ docker-compose exec php grep www-data /etc/passwd
www-data:x:1000:1000:Linux User,,,:/home/www-data:/sbin/nologin

And here is a demonstration of the fine problem:

$ docker-compose exec php ./artisan make:model Foo -m
Model created successfully.
Created Migration: 2020_09_25_051836_create_foos_table

$ docker-compose exec php ls -ln /var/www/html/database/migrations/2020_09_25_051836_create_foos_table.php
-rw-r--r--    1 0        0              571 Sep 25 05:18 /var/www/html/database/migrations/2020_09_25_051836_create_foos_table.php

I had also tried prefixing the run command with env UID=1000 GID=1000 as suggested in some forums, however that had no effect. Thinking that maybe the CLI interpreter (/usr/local/bin/php) might not use FPM behind the scenes, I tried checking for PHP config that may be relevant but found none:

$ docker-compose exec php grep -rvE "^; " /etc/php7/ | grep "user\|group"
/etc/php7/php.ini:;user_ini.filename = ".user.ini"
/etc/php7/php.ini:;user_ini.filename =
/etc/php7/php.ini:;user_ini.cache_ttl = 300
/etc/php7/php.ini:;ignore_user_abort = On
/etc/php7/php.ini:;syslog.facility = user
/etc/php7/php.ini:user_dir =
/etc/php7/php.ini:;odbc.default_user  =  Not yet implemented
/etc/php7/php.ini:;ibase.default_user =
/etc/php7/php.ini:mysqli.default_user =

How can I configure PHP to write files as user www-data, i.e. UID 1000?

1 Answer 1


You can use "user" in the docker compose yml file and specify the userid:groupid

user: "1000:1000"
  • Thank you Sandeep!
    – dotancohen
    Sep 25, 2020 at 6:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .