DEV Community

Discussion on: Docker, Docker Compose and permissions

Collapse
 
chrisjimallen profile image
Chris Allen

I've been struggling with this issue for a week, after switching from Vagrant. I'll try this with my WordPress site and see how I get on. One issue I need to consider is making the nginx container be able to write to the files, eg. when installing plugins or saving permalinks etc.

Currently, my host file permissions for my files are chrisallen:chrisallen, so will I need to chown them to be chrisallen:www-data, in order for my container to have write access? Or perhaps I could try just adding my own host user to the www-data group.

Did you find a workable solution for the above?

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.