大网数据广告活动
服务器运维问答

How to change permission of a folder to 777 in Dockerfile?

· 52CE 我爱测

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:…

问题原文

原帖链接 · ken · CC BY-SA 4.0

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. But seem 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)

已采纳解答

原帖链接 · Zoredache · CC BY-SA 4.0

You really shouldn't set 777, it would probably be better to just change the ownership of that directory to the www account.

Anyway your changes in the Dockerfile really don't matter, because you have a volume (appdata:/var/www) meaning that the permissions you have in the image are masked by your volume.

Your docker exec -it myapp /bin/sh would be failing because that image is running as www which won't be able to run chmod on file not owned by www.

Anyway try just mapping volume into another image temporarily. Do something like docker run --rm -it -v appdata:/var/www debian:buster-slim to start an interactive session to a Debian image with the volume mapped in. That image defaults to running as root, so you should be able to change/fix permissions in the volume. You could also pass the --user option when when running your docker exec.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-03-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。