Home > Blockchain >  Docker php apache permission denied
Docker php apache permission denied

Time:12-16

I'm working on configuring a docker setup for an old php project with apache, php 7.2 and mariadb. After I got the container up and running I get permission denied when I try to write a file from php.

What is the best approach to solve this?

docker-compose.yml

version: "3"

networks:
  dirtbike:

services:
  webserver:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: dirtbike-webserver
    restart: 'always'
    depends_on:
      - database
    ports:
      - "80:80"
      - "443:443"
    networks:
      - dirtbike
    volumes:
      - ./public_html:/var/www/html
  database:
    image: mariadb:10.3
    container_name: dirtbike-database
    restart: 'always'
    networks:
      - dirtbike
    ports:
      - "127.0.0.1:3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}

Dockerfile

FROM php:7.2-apache-stretch

RUN docker-php-ext-install pdo_mysql

RUN a2enmod ssl && a2enmod rewrite && a2enmod headers
RUN mkdir -p /etc/apache2/ssl
COPY ./Docker/ssl/*.pem /etc/apache2/ssl/

COPY ./Docker/config/apache/dirtbike.conf /etc/apache2/sites-available/000-default.conf

index.php

<?php

file_put_contents(__DIR__.DIRECTORY_SEPARATOR.'test.txt', 'lorem ipsum');

CodePudding user response:

The problem was that the user from the container (www-root) was different then the user from the host. I added args: uid: ${UID} in the docker-compose file like this:

version: "3"

networks:
  dirtbike:

services:
  webserver:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        uid: ${UID}
    container_name: dirtbike-webserver
    restart: 'always'
    depends_on:
      - database
    ports:
      - "80:80"
      - "443:443"
    networks:
      - dirtbike
    volumes:
      - ./public_html:/var/www/html

in Dockerfile I added ARG uid and RUN usermod -u ${uid} www-data && groupmod -g ${uid} www-data; like this:

FROM php:7.2-apache-stretch

ARG uid

RUN docker-php-ext-install pdo_mysql

RUN a2enmod ssl && a2enmod rewrite && a2enmod headers
RUN mkdir -p /etc/apache2/ssl
COPY ./Docker/ssl/*.pem /etc/apache2/ssl/

COPY ./Docker/config/apache/dirtbike.conf /etc/apache2/sites-available/000-default.conf

RUN usermod -u ${uid} www-data \
    && groupmod -g ${uid} www-data;

CodePudding user response:

In your docker compose file you can to add your local machine user. First you need check current user id, in my case it is ubuntu:

echo ${UID}

Output:

1001

docker-compose.yml:

webserver:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: dirtbike-webserver
    restart: 'always'
    depends_on:
      - database
    ports:
      - "80:80"
      - "443:443"
    networks:
      - dirtbike
    user: 1001 # local machine user id
    volumes:
      - ./public_html:/var/www/html

Hope help you.

  • Related