首页 > 解决方案 > --allow-root 在 docker 容器中运行 wp-cli 不起作用

问题描述

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

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 中输入的内容

伙计们,你有什么建议可以解决这个问题吗?

标签: wordpressbashdockeralpinewp-cli

解决方案


您正在与经典难题作斗争:什么进入,什么进入,bashrc什么bash_profile时候加载?

极短的版本是:

$HOME/.bash_profile:在登录 shell 处读取。应始终 source $HOME/.bashrc。应该只包含可以传递给其他函数的环境变量。

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

这对 OP 有何帮助?

OP 执行以下行:

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

sudo-command的标志-i启动登录外壳

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

所以 OP 启动了一个登录 shell,它只读取.bash_profile. 现在解决问题的方法是按照强烈推荐.bashrc的方式在其中获取文件。

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

有关点文件的更多信息:

相关文章:


推荐阅读