1

I have a project directory like this:

|-var/www
|-docker-compose.yml
|-app
|--uploads
|---photos
|-Dockerfile

This is my docker-compose.yml file:

myapp:
    build:
      context: myfolder
      dockerfile: Dockerfile
    container_name: flask
    image: api/v1:01
    restart: unless-stopped
    environment:
      APP_ENV: "prod"
      APP_DEBUG: "False"
      APP_PORT: 5000
    volumes:
      - appdata:/var/www

What I want: I want to change app/uploads/photos this folder's permission to 777.This is an upload folder,so user can upload to this folder.

My Dockerfile now is look like this:

FROM python:3.6.8-alpine3.9

ENV GROUP_ID=1000 \
    USER_ID=1000

WORKDIR /var/www/
ADD . /var/www/

RUN apk add --no-cache build-base libffi-dev openssl-dev ncurses-dev
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

RUN addgroup -g $GROUP_ID www
RUN adduser -D -u $USER_ID -G www www -s /bin/sh

USER www

EXPOSE 5000

After looking in this question,In order to achieve what I want,I tried below:

FROM python:3.6.8-alpine3.9

ENV GROUP_ID=1000 \
    USER_ID=1000

WORKDIR /var/www/
ADD . /var/www/

RUN apk add --no-cache build-base libffi-dev openssl-dev ncurses-dev
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

RUN addgroup -g $GROUP_ID www
RUN adduser -D -u $USER_ID -G www www -s /bin/sh

RUN chown -R www:www /var/www
RUN chmod -R 777 /var/www/uploads
RUN chmod -R 777 /var/www/uploads/photos


USER www

EXPOSE 5000

But seems like the chmod command in my dockerfile is not taking effect.Cause whenever I upload some files to app/uploads/photos in my code,my nginx server keep getting this error:

PermissionError: [Errno 13] Permission denied: '/var/www/uploads/photos/myfilename.png'

Somebody please help.Please provide a solution for how to change the permission of a folder in Dockerfile.

UPDATE:

I tried to change the permission of /var/www/uploads after build the container and the container is running by doing below:

docker exec -it myapp /bin/sh

then run

chmod -R 777 /var/www/uploads

What I get is chmod: /var/www/uploads: Operation not permitted

Therefore I suspect the this error will also happened when the docker is building,then according to this answer from serverfault, I tried to modify the dockerfile to this:

FROM python:3.6.8-alpine3.9


ENV GROUP_ID=1000 \
    USER_ID=1000

WORKDIR /var/www/
ADD . /var/www/

USER root
RUN chmod -R 777 /var/www/uploads

RUN addgroup -g $GROUP_ID www
RUN adduser -D -u $USER_ID -G www www -s /bin/sh

USER www

RUN apk add --no-cache build-base libffi-dev openssl-dev ncurses-dev
RUN pip install --upgrade pip
RUN pip install -r requirements.txt


EXPOSE 5000

But it still doesnt work.Look like my above approach is also wrong.Cause I already run in root in the dockerfile.But at the same time,when I access the container in host using docker exec,also getting Operation not permitted.

I am very new in Docker.Just cant figure it out how to get this done.

What I hope to know:

1) How to change the folder var/www/uploads to permission 777?

2) What is the problem causing I cant change the permission from my approach?

3) What is the better way to achieve this? (If any)

ken
  • 2,426
  • 5
  • 43
  • 98
  • Prhaps it is a dublette of this: https://stackoverflow.com/questions/45972608/how-to-give-folder-permissions-inside-a-docker-container-folder ? Switch User to admin:admin – nologin Mar 14 '20 at 11:35
  • How are you running the container? If you're mounting a host directory over the image content (`docker run -v` option or Docker Compose `volumes:` option) then nothing you do in the Dockerfile will matter, it will all get hidden. – David Maze Mar 14 '20 at 11:38
  • @DavidMaze I have a `docker-compose.yml` as well. the volume is defined there..I upload that as well – ken Mar 14 '20 at 11:42
  • With that setup the contents of the `appdata` named volume will hide everything the Dockerfile sets up in that directory tree. You'll need to change permissions after container startup, or set the container to run as a user who can access the volume content. – David Maze Mar 14 '20 at 11:56
  • @DavidMaze can you give me an example for how to that or what is the better to do that? Cause i totally new in docker.. – ken Mar 14 '20 at 13:02
  • Since you're already `COPY` the application code into your image, you don't need to overwrite it at runtime. I'd recommend deleting the `volumes:` block from your `docker-compose.yml`, and then the permission settings in your Dockerfile will be visible. – David Maze Mar 14 '20 at 13:19
  • @DavidMaze so u mean when I delete the `volumes:` block form `docker-compose.yml`,then the `chmod` in `dockerfile` will become work? – ken Mar 14 '20 at 13:23

0 Answers0