skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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"]
    

  2. And how about this?

    # Add www-data to the '1000-group'
    RUN usermod -u 1000 www-data
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search