PHP无法在Docker容器中写入 [英] PHP cannot write in docker container

查看:421
本文介绍了PHP无法在Docker容器中写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在docker中创建工作流程。
很简单:只需PHP和MySQL即可进行少量测试。

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

我的 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

和我的 Dockerfile

FROM php:5.6-apache

RUN docker-php-ext-install mysqli

没关系:我可以打开容器,但是PHP本身不能写在目录中(例如 fwrite() 或类似)。

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...

推荐答案

Apache使用用户运行PHP www-data ,此用户需要在您的主机目录中具有写权限。/public_html

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

要解决此问题,请转到docker-compose目录并执行以下命令,以更改 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

之后,您需要允许 www-data组中的用户编辑文件

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 {} \;






为当前用户编辑这些文件您需要将其添加到 www-data 组:

sudo usermod -aG www-data $USER

这篇关于PHP无法在Docker容器中写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆