DEV Community

Discussion on: Docker, Docker Compose and permissions

Collapse
 
malteriechmann profile image
Malte Riechmann • Edited

Hi Chris, we have a working solution for our local development environment. We just tell Nginx and PHP-FPM to run as »chrisallen«. Below you can have a look at a simplified version of our Dockerfile.

# Define base image
FROM php:8.1-fpm-alpine

# Define build arguments
ARG USER_ID
ARG GROUP_ID

# Define environment variables
ENV USER_NAME=chrisallen
ENV GROUP_NAME=chrisallen
ENV USER_ID=$USER_ID
ENV GROUP_ID=$GROUP_ID
ENV USER_ID=${USER_ID:-1001}
ENV GROUP_ID=${GROUP_ID:-1001}

# Add group and user based on build arguments
RUN addgroup --gid ${GROUP_ID} ${GROUP_NAME}
RUN adduser --disabled-password --gecos '' --uid ${USER_ID} --ingroup ${GROUP_NAME} ${USER_NAME}

# Set user and group of working directory
RUN chown -R chrisallen:chrisallen /var/www/html

# Update and install packages
# […]

# Install PHP extensions
# […]

# Set nginx and PHP-FPM user
RUN sed -ri -e "s!user nginx!user ${USER_NAME}!g" /etc/nginx/nginx.conf
RUN sed -ri -e "s!user = www-data!user = ${USER_NAME}!g" /usr/local/etc/php-fpm.d/www.conf
RUN sed -ri -e "s!group = www-data!group = ${GROUP_NAME}!g" /usr/local/etc/php-fpm.d/www.conf

# Manualy expose port 80 for outside access to nginx
# […]

# Copy configuration to application container
# […]

# Start nginx and PHP-FPM
# […]

Enter fullscreen mode Exit fullscreen mode

I hope this will help you.

Happy coding :)

Collapse
 
chrisjimallen profile image
Chris Allen

This is such a huge help, thanks for the personalised script ;) I'll try it out on the next setup.