fro*_*one 4 wordpress bash docker wp-cli alpine-linux
在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/
Run Code Online (Sandbox Code Playgroud)
我的.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
}
Run Code Online (Sandbox Code Playgroud)
当我尝试执行任何 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>
Run Code Online (Sandbox Code Playgroud)
看起来该命令行没有考虑我在 .bashrc 中输入的内容
伙计们,您对如何解决这个问题有什么建议吗?
您正面临着一个经典难题:放入什么bashrc
、放入什么bash_profile
以及何时加载哪个?
最短的版本是:
$HOME/.bash_profile
:在登录 shell 中读取。应始终来源$HOME/.bashrc
。应该只包含可以传递给其他函数的环境变量。
$HOME/.bashrc
:只读未登录的交互式 shell(例如,在 X 中打开终端)。应该只包含别名和函数
这对 OP 有什么帮助?
OP执行以下行:
$ sudo -u USER -i -- wp <command>
Run Code Online (Sandbox Code Playgroud)
sudo 命令的标志-i
启动登录 shell
-i, --login
:运行由目标用户的密码数据库条目指定的 shell 作为登录 shell。这意味着shell 将读取特定于登录名的资源文件,例如.profile
、.bash_profile
或。.login
如果指定了命令,则会通过 shell 的-c
选项将其传递到 shell 执行。如果未指定命令,则执行交互式 shell。
因此OP启动一个登录shell,它只读取.bash_profile
. 解决该问题的方法是现在强烈建议.bashrc
在其中获取文件。
# .bash_profile
if [ -n "$BASH" ] && [ -r ~/.bashrc ]; then
. ~/.bashrc
fi
Run Code Online (Sandbox Code Playgroud)
有关点文件的更多信息:
相关文章: