【问题标题】:How can one influence the order of RUN commands in a Dockerfile?如何影响 Dockerfile 中 RUN 命令的顺序?
【发布时间】:2016-07-11 01:31:31
【问题描述】:

我正在编写一个 Dockerfile,其中 root 用户创建了一个名为 blog 的用户来管理网站部署。我使用 Docker Hub wordpress 容器作为基础。 root 用户在/var/www/html 下创建文件夹,并授予blog 用户在其下写入的权限。

USER blog 之后的下一组 RUN 命令中,这些命令不会确认之前blog 用户被授予写入/var/www/html 权限的状态。该用户需要在其中克隆一个 git 存储库,但我收到错误 fatal: could not create leading directories of '/var/www/html/wp-content/uploads': Permission denied,因为尽管之前为该用户设置了权限,但新用户无法在那里写入。

以下是我用来创建blog 用户、将文件复制到他们的主目录、然后使用该用户克隆一个 repo 的命令:

ENV WORDPRESS_DB_USER=wp_blog \
WORDPRESS_DB_NAME=wp_blog \
WORDPRESS_DIR=/var/www/html \
TERM=xterm

# Setup the WordPress site user
# Preliminary command
RUN useradd --create-home --shell /bin/false --groups www-data blog    

COPY id_rsa* known_hosts /home/blog/.ssh/ 
## First set of commands
RUN mkdir --parents "$WORDPRESS_DIR"/wp-content/uploads \
&& chown --recursive blog:blog "$WORDPRESS_DIR" \
&& chown --recursive blog:blog /home/blog

USER blog 
## Second set of commands
RUN chmod u=rw,g=,o= /home/blog/.ssh/id_rsa \
&& chmod u=rw,g=r,o=r /home/blog/.ssh/id_rsa.pub \
&& chmod u=rw,g=r,o=r /home/blog/.ssh/known_hosts \
&& eval $(ssh-agent -s) \
&& ssh-add \
&& export PATH=$PATH:/usr/sbin \

# Use the WordPress user to download the content repo and hooks 
# The following command results in a permissions error:   
&& git clone git@gitlab.com:jb-merideoux/jbm-uploads.git "$WORDPRESS_DIR"/wp-content/uploads \
&& mkdir --parents /home/blog/git/wpgithooks \
&& cd /home/blog/git/wpgithooks \
&& git clone git@github.com:enderandpeter/wpgithooks.git /home/blog/git/wpgithooks \
&& git checkout --git-dir=/home/blog/git/wpgithooks/.git --track origin/wpaddons \
&& chmod u+x /home/blog/git/wpgithooks/*.sh \
&& echo Run the script at /home/blog/git/wpgithooks/setup.sh to get started

blog 用户可以为复制的 SSH 密钥对设置权限,但如果chown --recursive blog:blog /home/blog 命令不存在,则不能。我认为之前的chown --recursive blog:blog "$WORDPRESS_DIR" 命令对于blog 用户是否可以在blog 更改由@ 复制到其主页的文件的权限的同一组命令中写入/var/www/html 具有相同的影响在root 用户之后的987654338@ 确保/home/blog 下的所有内容都归blog 所有。不知何故,/home/blog 的权限在第二组 RUN 命令中有效,但/var/www/html 的权限无效。

经过仔细检查,mkdir --parents "$WORDPRESS_DIR"/wp-content/uploads 指令似乎也没有运行,因为当我在 git clone 命令之前添加一些东西来创建像touch "$WORDPRESS_DIR"/wp-content/uploads/afile 这样的文件时,错误是touch: cannot touch '/var/www/html/wp-content/uploads/afile': No such file or directory

当我尝试在/var/www/html/ 中创建afile(它已经存在,因为它是由基础 wordpress 容器创建的)时,我收到错误touch: cannot touch '/var/www/html/afile': Permission denied,表明chown --recursive blog:blog "$WORDPRESS_DIR" 命令未被确认。在第一组 RUN 语句中的三个命令中,第二组 RUN 语句中唯一确认的似乎是 chown --recursive blog:blog /home/blog

如何编写这个 Dockerfile,以便在新用户执行第二组 RUN 语句时,第一个 RUN 语句中的所有命令都已发出?

【问题讨论】:

  • 查看此链接。这很可能不是权限问题,而是 ssh-key 问题docunext.com/blog/2011/02/…
  • 感谢您的建议!我会看看。我发现了另一个 StackOverflow 问题,我现在没有找到,这也表明了这一点。我用一些观察结果编辑了我的问题,这些观察结果让我确信,在发出第二组命令时,只有第一组命令中的一个被执行。

标签: linux file docker permissions dockerfile


【解决方案1】:

哇,太激烈了。第二组命令只是从不了解事先所做的事情。我已经看到了后续 RUN 命令知道之前发生的事情的例子,所以我仍然不清楚它是如何确定的。

我找到的解决方案是在第一组命令中使用su,就在root创建目录并设置权限之后:

RUN mkdir --parents "$WORDPRESS_DIR"/wp-content/uploads \
    && chown --recursive blog:blog "$WORDPRESS_DIR" \
    && chown --recursive blog:blog /home/blog \
    && su - blog -s /bin/bash -c '\
        chmod u=rwx,g=,o= /home/blog/.ssh \
        && chmod u=rw,g=,o= /home/blog/.ssh/id_rsa \
...
    && git clone git@gitlab.com:jb-merideoux/jbm-uploads.git '"$WORDPRESS_DIR"'/wp-content/uploads \
...
    && echo '"$WORDPRES_DIR"' setup complete.'

如您所见,这是一些疯狂的引用,但幸运的是,您可以完全格式化命令,就好像它们被直接提供给 Dockerfile。我确实在最后使用了USER blog 来做不依赖于早期东西的事情,但我想有时使用 Docker,你必须将一大堆命令串在一起,但我可能可以把所有这些变成一个单独的脚本...

【讨论】:

    猜你喜欢
    • 2017-01-22
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2019-10-19
    相关资源
    最近更新 更多