【问题标题】:How can I change permission on folder inside Dockerfile如何更改 Dockerfile 中文件夹的权限
【发布时间】:2020-09-23 09:23:17
【问题描述】:

我试图在 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

提前谢谢你。

【问题讨论】:

    标签: docker heroku dockerfile heroku-cli


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2019-09-26
      • 2020-03-19
      • 2012-02-16
      • 2012-04-27
      • 2022-10-17
      • 2019-12-06
      • 2018-09-06
      • 1970-01-01
      • 2014-03-02
      相关资源
      最近更新 更多