简体   繁体   中英

Docker / PHP / Apache - File permissions lost when created inside container

i'm trying to set up a docker app with Apache and Php stack. But when i create a new file inside my project container, the permissions losted because root user is connected by default. How can I connect my host user?

docker-compose.yml

version: '3'
services:
  web:
    container_name: web
    build:
      context: docker/web
    volumes:
      - ./app/:/var/www/app
      - ./docker/web/apache/log:/var/log/apache2/app
    working_dir: /var/www/app
    ports:
      - 80:80

dockerfile

FROM php:7.4-apache

ENV APACHE_DOCUMENT_ROOT /var/www

RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libzip-dev \
        git \
        zip \
        unzip \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install zip pdo pdo_mysql opcache \
    && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime
RUN "date"


RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

COPY php/php.ini /usr/local/etc/php/conf.d/app.ini
COPY apache/app.conf /etc/apache2/sites-available/000-default.conf

RUN groupadd dev -g 1000
RUN useradd dev -g dev -d /home/dev -m
RUN echo '%dev ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

I tried to create a dev user in my Dockerfile but not working.

And how about this?

# Add www-data to the '1000-group'
RUN usermod -u 1000 www-data

I solved the problem by adding my dev user to sudoers and then running apache2ctl in sudo.

FROM php:7.4-apache

RUN apt-get update && apt-get install -y \
        sudo \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libzip-dev \
        git \
        zip \
        unzip \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install zip pdo pdo_mysql opcache \
    && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime
RUN "date"


RUN sed -ri -e 's!/var/www/html!/var/www!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!/var/www!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

COPY php/php.ini /usr/local/etc/php/conf.d/app.ini
COPY apache/app.conf /etc/apache2/sites-available/000-default.conf

RUN groupadd dev -g 1000
RUN useradd dev -g dev -d /home/dev -m
RUN echo '%dev ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER dev:dev

CMD ["sudo", "/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

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