Mário Valney
Mário Valney

Reputation: 133

PHP cannot write in docker container

I'm trying to create a workflow in docker. It's simple: just PHP and MySQL to little tests.

My docker-compose.yml:

web:
  build: .docker
  links:
    - mysql:mysql
  ports:
    - "80:80"
    - "443:443"
  volumes:
    - ./public_html:/var/www/html
mysql:
  image: mysql:5.7
  environment:
    - MYSQL_ROOT_PASSWORD=root
  volumes:
    - "./.data/db:/var/lib/mysql"
phpmyadmin:
  image: phpmyadmin/phpmyadmin
  ports:
    - "8080:80"
  links:
    - mysql:mysql
  environment:
    - PMA_HOST=mysql
  volumes:
    - /sessions

And my Dockerfile:

FROM php:5.6-apache

RUN docker-php-ext-install mysqli

It's all ok: I can up containers but PHP itself cannot write in directories (like fwrite() or similar).

I researched a lot and tried a lot of tutorials, but nothing...

Upvotes: 6

Views: 5912

Answers (1)

ChVuagniaux
ChVuagniaux

Reputation: 114

Apache runs PHP with the user www-data, this user needs to have write access on your host directory ./public_html

To fix that, go to your docker-compose directory and execute the following command to change the owner of your public_htmldirectory and all files inside.

sudo chown -R www-data:www-data public_html

After that you need to allow users in the group "www-data" to edit files

# To change all the directories to 775 
# (write for user & group www-data, read for others):
find public_html -type d -exec chmod 775 {} \;

# To change all the files to 664
# (write for user & group www-data, read for others):
find public_html -type f -exec chmod 664 {} \;

In order for your current user to edit these files you need to add it to the www-data group :

sudo usermod -aG www-data $USER

Upvotes: 9

Related Questions