PHP write to Docker Volume Permission Denied

  apache, containers, docker, dockerfile, php

I am trying to attach a volume to my Docker container for a PHP app that has an upload functionality.

Every time I boot up the Docker image and try to upload, I get:

Warning: move_uploaded_file(/data/projectname/uploads/CtnXkdonnhMLskxb.pdf): failed to open stream: Permission denied in /var/www/html/phpapi/upload.php on line 63

I’ve scoured a lot of the permissions SO articles (this, this, this, this, and this) and have found nothing that can help.

It seems my volume is attached properly. I see this when I inspect my Container:

"Mounts": [
  {
     "Type": "volume",
     "Name": "my-project-volume",
     "Source": "/var/lib/docker/volumes/my-project-volume/_data",
     "Destination": "/data/projectname/uploads",
     "Driver": "local",
     "Mode": "z",
     "RW": true,
     "Propagation": ""
  }
],

And this is my Dockerfile:

FROM node:12-alpine as build
RUN mkdir /frontend
WORKDIR /frontend
COPY ./react /frontend
RUN yarn install
RUN yarn build

FROM php:7.3-apache
COPY --from=build /frontend/build /var/www/html
COPY ./php /var/www/html/phpapi
EXPOSE 8080

My Docker run command: docker run -p 3000:80 -i -d -v my-project-volume:/data/projectname/uploads project-name:latest

When I go into my container and run ls -l on any file/folder in my container, I see that the permissions are root. For example ls -l on the data folder that’s the mounted volume:

drwxr-xr-x 3 root root 4096 Dec  8 22:45 projectname

Can someone explain to me like I’m five what is happening and how to possibly fix it? I suspect it has to do with the fact that my host permissions is not the same as the container permissions… is that right?

Source: Docker Questions

LEAVE A COMMENT