如何使用docker-compose将用户上传的文件保存到已安装的卷中? [英] How to save user uploaded files to mounted volume using PHP, docker-compose?

查看:106
本文介绍了如何使用docker-compose将用户上传的文件保存到已安装的卷中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置有docker-compose的LAMP堆栈,并且在将用户上传的文件保存到安装的卷以进行持久存储方面遇到问题.这是我的撰写文件:

I have a LAMP stack configured with docker-compose, and am having issues saving user uploaded files to a mounted volume for persistent storage. Here is my compose file:

    version: '3.7'
services:

  php-apache:
    build: './php-apache'
    restart: always
    ports:
      - 5000:80
    volumes:
      - ./public_html:/var/www/html
      - ./composer/vendor:/var/www/html/vendor
      - ./tmp:/usr/local/tmp
      - ./cert/:/usr/local/apache2/cert
      - covers:/var/lib/app-data/covers:rw
      - ebooks:/var/lib/app-data/ebooks:rw
    depends_on: 
      - mysql
      - composer

  mysql:
    build: './mysql'
    restart: always
    ports:
      - '33061:3306'
    volumes:
      - ./database:/var/lib/mysql
      - ./mysql/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    environment: 
      MYSQL_ROOT_PASSWORD: secretpw
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypw 
      MYSQL_DATABASE: mydb

  composer:
    build: './composer'
    working_dir: /composer
    volumes:
      - ./composer:/composer
    command: install

volumes:
  covers: {}
  ebooks: {}

在我的PHP代码中,我尝试将文件保存到卷中:

In my PHP code, I attempt to save files to the volumes:

    if (!move_uploaded_file($_FILES[$file]['tmp_name'], $path . $filename)) {
        throw new RuntimeException("Failed to move uploaded file.");
    }

然后出现以下错误:

警告:move_uploaded_file(/var/lib/app-data/covers/useruploadedfile.jpg):无法打开流:权限被拒绝

Warning: move_uploaded_file(/var/lib/app-data/covers/useruploadedfile.jpg): failed to open stream: Permission denied

我试图将卷的权限更改为无效.有什么方法可以授予PHP对已安装卷的写入权限?

I have attempted to change permissions on the volume to no avail. Is there any way to give PHP write permissions to mounted volumes?

推荐答案

我在@Nguyen的指导下找到了一个解决方案,尽管我的情况还有很多,所以我想我会在这种情况可能会帮助某人.

I found a solution with the guidance of @Nguyen, although there was a bit more to it in my situation so I thought I would write my own detailed answer in case it may help someone.

如果在MacOS上使用Docker,则Docker实际上是在Linux VM上运行的,而不是在本机上本地运行的.尝试更改主机上的权限时,这使我震惊(从docker的角度来看,主机是VM).

If you are using Docker on MacOS, Docker is actually running on a Linux VM and not natively on the machine. This had tripped me up when attempting to change permissions on the host machine (the host machine from the perspective of docker is the VM).

我使用以下功能找到了正在执行PHP脚本的用户和组ID:

I found what user and group id was executing my PHP script using the following functions:

echo posix_getuid();
echo posix_getgid();

我使用以下命令找到了卷的安装位置:

I found out the mount location for my volume using the following command:

docker volume inspect nameofyourvolume

当我尝试修改该卷的安装目录时(/var/lib/docker/volumes),我得到的是无此类文件或目录".经过研究后,我意识到挂载位置的路径是相对于运行docker的VM的.我找到了一种使用以下命令进入此VM的方法(请参阅

When I tried to chown the directory that the volume was mounted in (/var/lib/docker/volumes) I was getting "no such file or directory". After doing some research, I realized that the path for the mount location was relative to the VM running docker. I found a way to get into this VM with the following command (see https://www.bretfisher.com/docker-for-mac-commands-for-getting-into-local-docker-vm/):

docker run -it --rm --privileged --pid=host justincormack/nsenter1

这会将您放入对VM具有完全权限的容器.然后,您可以照常更改对保存卷的目录的权限.

This drops you into a container with full permissions to the VM. You can then change permissions to the directory holding your volumes as usual.

这篇关于如何使用docker-compose将用户上传的文件保存到已安装的卷中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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