Mahadi Hassan
Mahadi Hassan

Reputation: 310

Permission Denied for mkdir() php in Docker

When i Run my php code in docker for mkdir it says this in the browser

Warning: mkdir(): Permission denied in /var/www/html/test.php on line 9

Warning: mkdir(): Permission denied in /var/www/html/test.php on line 12

I have been searching but can't get any suitable source for my use. How can i allow docker to give permission to my php or what is the solution?

Here is my php code

<?php
$postk = "1220";
$root = "login/";
$room_id = "foile";
$NewRoot = "movie";

if (!file_exists($root . $postk)) {
    $dirPathp = $root . $postk;
    $resultp = mkdir($dirPathp, 0777, true);

    $dirPathpr = $root . $postk . "/" . $room_id;
    $resultpr = mkdir($dirPathpr, 0777, true);

} else {
    $dirPathpr = $root . $postk . " / " . $room_id;
    $resultpr = mkdir($dirPathpr, 0777, true);
} ?>

Here is my Docker File

FROM php:7.3-apache

#Install git and Mysql Extensions for php

RUN apt-get update && apt-get install -y git
RUN docker-php-ext-install pdo pdo_mysql mysqli
RUN a2enmod rewrite

COPY . /var/www/html/
EXPOSE 80/tcp
EXPOSE 443/tcp

Here is Docker compose file

version: '3.8'

volumes:
  datafiles:

services:

  mysql:
    image: mysql:8.0.0
    container_name: mysql6
    environment:
      - MYSQL_ROOT_PASSWORD=sopno_1818120**
      - MYSQL_TCP_PORT=3306
      - TZ=Asia/Dhaka
    volumes:
      - datafiles:/var/lib/mysql
    restart: always

  website:
    container_name: php6
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - DB_HOST=mysql
      - MYSQL_DBPORT=3306
      - DB_USERNAME=sopnobari_tuneb
      - DB_PASSWORD=sopno_1818120**
      - DB_NAME=sopnobari_sopno
    ports:
      - "3050:80"
      - "3051:443"
    depends_on:
      - mysql

Upvotes: 2

Views: 5715

Answers (2)

DrGregoryHouse
DrGregoryHouse

Reputation: 615

This problem comes up because when you mount a volume into your docker container, the file and directory ownership of the host system passes along to the container aswell.

The files on your host system are owned by your user, PHP on the other hand runs on the user www-data. This differece causes the permission Problems.

Check this out to get rid of the problem.

Upvotes: 1

Mahadi Hassan
Mahadi Hassan

Reputation: 310

I got the solution

in docker file add this code

COPY . /var/www/html/
RUN chown -R www-data:www-data /var/www #this line after COPY
EXPOSE 80/tcp
EXPOSE 443/tcp 

Upvotes: 4

Related Questions