我在寫一個Dockerfile,其中root用戶創建一個名爲blog
的用戶來管理網站部署。我使用Docker Hub wordpress容器作爲基礎。根用戶在/var/www/html
下創建文件夾,並給出blog
用戶的權限以便在下面寫入。如何影響Dockerfile中RUN命令的順序?
在USER blog
之後的下一組運行命令中,這些命令不會確認blog
用戶被授予寫入權限/var/www/html
的先前狀態。這個用戶需要在那裏克隆一個git倉庫,但是由於新用戶無法在那裏寫入,所以我得到錯誤fatal: could not create leading directories of '/var/www/html/wp-content/uploads': Permission denied
,儘管以前爲這個用戶設置了權限。
下面是我用來創建blog
用戶的命令,文件複製到自己的主目錄,然後克隆與該用戶回購:
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 [email protected]:jb-merideoux/jbm-uploads.git "$WORDPRESS_DIR"/wp-content/uploads \
&& mkdir --parents /home/blog/git/wpgithooks \
&& cd /home/blog/git/wpgithooks \
&& git clone [email protected]: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
用戶是否可以在/var/www/html
中寫入相同的一組命令中具有相同的效果,其中blog
在root
用戶確認所有內容之後將對複製到其家庭的文件的權限改變/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密鑰問題http://www.docunext.com/blog/2011/02/fatal-could-not-create-leading-directories-of-permission-denied.html –
感謝您的建議!我會看一看。我發現了另一個StackOverflow問題,我現在還沒有找到,這也表明了這一點。我用一些觀察來編輯我的問題,這些觀察讓我相信,當發出第二組命令時,只有第一組命令中的一組已經被執行。 –