如何更改 Dockerfile 中文件夹的权限

How can I change permission on folder inside Dockerfile

我试图在 heroku 上推送容器,但在更改权限时出现此错误

FROM  ubuntu:latest

ENV PORT=80

RUN apt-get update \
    && apt-get install -y curl zip unzip  \
            php7.4 php7.4-fpm \
            nginx gettext-base sudo

COPY ./webapp /var/www/myapp

WORKDIR /var/www/myapp

COPY default.conf.template /etc/nginx/conf.d/default.conf.template

COPY nginx.conf /etc/nginx/nginx.conf

RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo

USER docker

RUN  chown -R www-data:www-data /var/www

RUN  chmod -R 755 /var/www

CMD /bin/bash -c "envsubst '$PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf" && nginx -g 'daemon off;'

我有这个错误

Step 10/12 : RUN  chown -R www-data:www-data /var/www
 ---> Running in c5952535d0cc
chown: changing ownership of '/var/www/html/index.nginx-debian.html': Operation not permitted
chown: changing ownership of '/var/www/html': Operation not permitted
chown: changing ownership of '/var/www/myapp/index.php': Operation not permitted
chown: changing ownership of '/var/www/myapp/.gitignore': Operation not permitted
chown: changing ownership of '/var/www/myapp': Operation not permitted
chown: changing ownership of '/var/www': Operation not permitted
The command '/bin/sh -c chown -R www-data:www-data /var/www' returned a non-zero code: 1
 !    Error: docker build exited with Error: 1

提前致谢。

USER docker

RUN  chown -R www-data:www-data /var/www

USER docker 将导致所有后续 RUN 命令成为 运行 作为 docker 用户(参见 USER docs),但是该用户没有权限在构建期间更改 /var/www 的所有权,因此您的错误。

在设置 docker 用户之前,您应该 运行 您的 chown 命令,例如:

# [ ... ]
COPY nginx.conf /etc/nginx/nginx.conf

RUN  chown -R www-data:www-data /var/www

RUN  chmod -R 755 /var/www

RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo

USER docker

甚至更好:

# [ ... ]
COPY nginx.conf /etc/nginx/nginx.conf

RUN chown -R www-data:www-data /var/www && \
  chmod -R 755 /var/www && \
  useradd -m docker && echo "docker:docker" | chpasswd && \
  adduser docker sudo

USER docker

哪个会 use less layers