--allow-root无法在docker容器中运行wp-cli

时间:2019-10-25 13:31:25

标签: wordpress bash docker alpine wp-cli

在Docker中使用WP CLI时,我需要以root用户身份执行。 我需要直接在.bashrc中添加标志--allow-root,并且试图弄清为什么它不起作用。

FROM webdevops/php-dev:7.3

# configure postfix to use mailhog
RUN postconf -e "relayhost = mail:1025"

# install wp cli
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
    chmod +x wp-cli.phar && \
    mv wp-cli.phar /usr/local/bin/wp && \
    echo 'wp() {' >> ~/.bashrc && \
    echo '/usr/local/bin/wp "$@" --allow-root' >> ~/.bashrc && \
    echo '}' >> ~/.bashrc

WORKDIR /var/www/html/

我的.bashrc

# ~/.bashrc: executed by bash(1) for non-login shells.

# Note: PS1 and umask are already set in /etc/profile. You should not
# need this unless you want different defaults for root.
# PS1='${debian_chroot:+($debian_chroot)}\h:\w\$ '
# umask 022

# You may uncomment the following lines if you want `ls' to be colorized:
# export LS_OPTIONS='--color=auto'
# eval "`dircolors`"
# alias ls='ls $LS_OPTIONS'
# alias ll='ls $LS_OPTIONS -l'
# alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
wp() {
/usr/local/bin/wp "$@" --allow-root
}

当我尝试执行任何wp命令时,出现此错误:

Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress installation exists under.

If you REALLY mean to run this as root, we won't stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS.

If you'd like to continue as root, please run this again, adding this flag:  --allow-root

If you'd like to run it as the user that this site is under, you can run the following to become the respective user:

    sudo -u USER -i -- wp <command>

看起来该命令行没有考虑我在.bashrc中输入的内容

伙计们,您对如何解决此问题有任何建议吗?

3 个答案:

答案 0 :(得分:4)

您正在为经典难题苦苦挣扎: bashrc中发生了什么,bash_profile中发生了什么,何时加载了哪个?

极端简短的版本是:

  

$HOME/.bash_profile :在登录Shell中阅读。应始终来源$HOME/.bashrc。应该只包含可以传递给其他函数的环境变量。

     

$HOME/.bashrc :仅针对未登录的交互式外壳                     (例如,在X中打开终端)。应该只包含别名和函数

这对OP有何帮助?

OP执行以下行:

$ sudo -u USER -i -- wp <command>

sudo命令的标志-i启动登录外壳程序

  

-i, --login:将目标用户的密码数据库条目指定的shell作为登录shell运行。这意味着Shell将读取特定于登录的资源文件,例如.profile.bash_profile.login。如果指定了命令,则通过外壳的-c选项将其传递到外壳以执行。如果未指定命令,则执行交互式外壳程序。

因此,OP启动了一个登录外壳程序,该外壳程序仅读取.bash_profile。解决问题的方法现在是像在strongly recommended中一样在其中获取.bashrc文件。

# .bash_profile
if [ -n "$BASH" ] && [ -r ~/.bashrc ]; then
    . ~/.bashrc
fi

有关点文件的更多信息:

相关帖子:

答案 1 :(得分:0)

问题是~/.bashrc没有被获取。它将仅来自交互式Bash shell。

通过可执行文件可能会获得更好的结果。像这样:

# install wp cli
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
    chmod +x wp-cli.phar && \
    mv wp-cli.phar /usr/local/bin/wp-cli.phar && \
    echo '#!/bin/sh' >> /usr/local/bin/wp && \
    echo 'wp-cli.phar "$@" --allow-root' >> /usr/local/bin/wp && \
    chmod +x /usr/local/bin/wp

答案 2 :(得分:0)

我最近遇到了同样的问题。在我的Dockerfile中,我正在运行:

RUN wp core download && wp plugin install woocommerce --activate --allow-root

我查看了错误消息,并认为从其措辞的角度来看,--allow-root在您第一次使用时会被忽略。所以我将其添加到第一个wp命令中,并且可以正常工作。

RUN wp core download --allow-root && wp plugin install woocommerce --activate --allow-root