How to change the nginx process user of the official docker image nginx?

FYI

  1. It is problem of php-fpm image
  2. It is not about usernames, it is about www-data user ID

What to do

Fix your php-fpm container and don't break good nginx container.

Solutions

  • Here is mine post with solution for docker-compose (nginx + php-fpm(alpine)): https://stackoverflow.com/a/36130772/1032085

  • Here is mine post with solution for php-fpm(debian) container: https://stackoverflow.com/a/36642679/1032085

  • Solution for Official php-fpm image. Create Dockerfile:

    FROM php:5.6-fpm
    RUN usermod -u 1000 www-data
    

I know the OP asked for a solution that doesn't extend the nginx image, but I've landed here without that constraint. So I've made this Dockerfile to run nginx as www-data:www-data (33:33) :

FROM nginx:1.17

# Customization of the nginx user and group ids in the image. It's 101:101 in
# the base image. Here we use 33 which is the user id and group id for www-data
# on Ubuntu, Debian, etc.
ARG nginx_uid=33
ARG nginx_gid=33

# The worker processes in the nginx image run as the user nginx with group
# nginx. This is where we override their respective uid and guid to something
# else that lines up better with file permissions.
# The -o switch allows reusing an existing user id
RUN usermod -u $nginx_uid -o nginx && groupmod -g $nginx_gid -o nginx

It accepts a uid and gid on the command line during image build. To make an nginx image that runs as your current user id and group id for example:

docker build --build-arg nginx_uid=$(id -u) nginx_uid=$(id -g) .

The nginx user and group ids are currently hardcoded to 101:101 in the image.

Tags:

Docker

Nginx